-
Notifications
You must be signed in to change notification settings - Fork 6
[CQT-342] Implement two qubit canonical decomposition into CZ gates #684
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
elenbaasc
merged 10 commits into
develop
from
CQT-342-Implement-two-qubit-canonical-decomposition-into-CZ-gates
May 28, 2026
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
74e85a5
Start on can2cz_decomposer.
elenbaasc 13a6a17
Implemented Can2CZDecomposer.
elenbaasc 1f98f60
Add simple tests (CNOT test does not pass yet).
elenbaasc 35da787
Add K-rotations to decomposition.
elenbaasc 7e9106f
Add tests of different gate semantics.
elenbaasc b283753
Fix linting and typing.
elenbaasc 334fe07
[WIP] Trying to fix semantics qubit ordering.
elenbaasc 9b06618
Build controlled matrix according to operand order convention.
elenbaasc ae4d5a4
Resolve review comments.
elenbaasc 77c3fbf
Merge develop into feature branch.
elenbaasc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from math import pi | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| import numpy as np | ||
|
|
||
| from opensquirrel import CNOT, CZ, X90, H, MinusX90, Ry, S, SDagger, Z | ||
| from opensquirrel.ir.semantics.bsr import BlochSphereRotation | ||
| from opensquirrel.ir.semantics.canonical_gate import CanonicalAxis | ||
| from opensquirrel.ir.single_qubit_gate import SingleQubitGate | ||
| from opensquirrel.ir.two_qubit_gate import TwoQubitGate | ||
| from opensquirrel.passes.decomposer.general_decomposer import Decomposer | ||
|
|
||
| if TYPE_CHECKING: | ||
| from opensquirrel.ir import Gate | ||
|
|
||
|
|
||
| class Can2CZDecomposer(Decomposer): | ||
| def decompose(self, instruction: Gate) -> list[Gate]: | ||
| """General decomposition of an arbitrary 2-qubit gate into (at most 3) CZ gate(s) with single-qubit rotations. | ||
|
|
||
| Adapted from [Quantum Gates by G.E. Crooks (2024), Section 7.3](https://threeplusone.com/pubs/on_gates.pdf). | ||
|
|
||
| Note: | ||
| This decomposition does not, in general, preserve the global phase of the original gate. | ||
| It is advised to run the single-qubit gates merger pass after this two-qubit gate decomposition pass. | ||
|
|
||
| Args: | ||
| instruction (Gate): 2-qubit gate to decompose. | ||
|
|
||
| Returns: | ||
| Decomposition of the original gate into a sequence of gates. | ||
|
|
||
| """ | ||
| if not isinstance(instruction, TwoQubitGate): | ||
| return [instruction] | ||
|
|
||
| gate = instruction | ||
| q0, q1 = gate.qubit_operands | ||
|
|
||
| if gate == CZ(q0, q1) or gate == CZ(q1, q0): | ||
| return [gate] | ||
|
|
||
| if gate == CNOT(q0, q1): | ||
| return [Ry(q1, -pi / 2), CZ(q0, q1), Ry(q1, pi / 2)] | ||
|
|
||
| gate_axis = gate.canonical.axis | ||
| gate_rotations = gate.canonical.rotations | ||
| if gate_rotations is None: | ||
| gate_rotations = [BlochSphereRotation(axis=(1, 0, 0), angle=0, phase=0)] * 4 | ||
| k1 = SingleQubitGate(q0, gate_rotations[0]) | ||
| k2 = SingleQubitGate(q1, gate_rotations[1]) | ||
| k3 = SingleQubitGate(q0, gate_rotations[2]) | ||
| k4 = SingleQubitGate(q1, gate_rotations[3]) | ||
|
|
||
| if gate_axis == CanonicalAxis((0.5, 0.0, 0.0)): | ||
| return [ | ||
| k1, | ||
| k2, | ||
| H(q0), | ||
| S(q0), | ||
| H(q1), | ||
| S(q1), | ||
| H(q1), | ||
| Ry(q1, -pi / 2), | ||
| CZ(q0, q1), | ||
| Ry(q1, pi / 2), | ||
| H(q0), | ||
| k3, | ||
| k4, | ||
| ] | ||
| if np.isclose(gate_axis[2], 0): | ||
| tx, ty, _ = gate_axis.value | ||
| Xtx = SingleQubitGate(q0, BlochSphereRotation(axis=(1, 0, 0), angle=pi * tx, phase=pi / 2 * tx)) # noqa: N806 | ||
| Zty = SingleQubitGate(q1, BlochSphereRotation(axis=(0, 0, 1), angle=pi * ty, phase=pi / 2 * ty)) # noqa: N806 | ||
| return [ | ||
| k1, | ||
| k2, | ||
| Z(q0), | ||
| MinusX90(q0), | ||
| Z(q1), | ||
| MinusX90(q1), | ||
| Ry(q1, -pi / 2), | ||
| CZ(q0, q1), | ||
| Ry(q1, pi / 2), | ||
| Xtx, | ||
| Zty, | ||
| Ry(q1, -pi / 2), | ||
| CZ(q0, q1), | ||
| Ry(q1, pi / 2), | ||
| X90(q0), | ||
| Z(q0), | ||
| X90(q1), | ||
| Z(q1), | ||
| k3, | ||
| k4, | ||
| ] | ||
| tx, ty, tz = gate_axis.value | ||
| ztz = tz - 0.5 | ||
| ytx = tx - 0.5 | ||
| yty = 0.5 - ty | ||
| Ztz = SingleQubitGate(q0, BlochSphereRotation(axis=(0, 0, 1), angle=pi * ztz, phase=pi / 2 * ztz)) # noqa: N806 | ||
| Ytx = SingleQubitGate(q1, BlochSphereRotation(axis=(0, 1, 0), angle=pi * ytx, phase=pi / 2 * ytx)) # noqa: N806 | ||
| Yty = SingleQubitGate(q1, BlochSphereRotation(axis=(0, 1, 0), angle=pi * yty, phase=pi / 2 * yty)) # noqa: N806 | ||
| return [ | ||
| k1, | ||
| k2, | ||
| S(q1), | ||
| Ry(q0, -pi / 2), | ||
| CZ(q1, q0), | ||
| Ry(q0, pi / 2), | ||
| Ztz, | ||
| Ytx, | ||
| Ry(q1, -pi / 2), | ||
| CZ(q0, q1), | ||
| Ry(q1, pi / 2), | ||
| Yty, | ||
| Ry(q0, -pi / 2), | ||
| CZ(q1, q0), | ||
| Ry(q0, pi / 2), | ||
| SDagger(q0), | ||
| k3, | ||
| k4, | ||
| ] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from math import pi | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| import numpy as np | ||
| import pytest | ||
|
|
||
| from opensquirrel import CNOT, CR, CZ, SWAP, CRk, H, Ry | ||
| from opensquirrel.ir.semantics.bsr import BlochSphereRotation | ||
| from opensquirrel.ir.semantics.canonical_gate import CanonicalGateSemantic | ||
| from opensquirrel.ir.semantics.controlled_gate import ControlledGateSemantic | ||
| from opensquirrel.ir.semantics.matrix_gate import MatrixGateSemantic | ||
| from opensquirrel.ir.two_qubit_gate import TwoQubitGate | ||
| from opensquirrel.passes.decomposer import Can2CZDecomposer | ||
| from opensquirrel.passes.decomposer.general_decomposer import check_gate_decomposition | ||
|
|
||
| if TYPE_CHECKING: | ||
| from opensquirrel.ir import Gate | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def decomposer() -> Can2CZDecomposer: | ||
| return Can2CZDecomposer() | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("gate", "expected_result"), | ||
| [ | ||
| (H(0), [H(0)]), | ||
| (Ry(0, 2.345), [Ry(0, 2.345)]), | ||
| ], | ||
| ids=["Hadamard", "rotation_gate"], | ||
| ) | ||
| def test_ignores_1q_gates(decomposer: Can2CZDecomposer, gate: Gate, expected_result: list[Gate]) -> None: | ||
| check_gate_decomposition(gate, expected_result) | ||
| assert decomposer.decompose(gate) == expected_result | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("gate", "expected_result"), | ||
| [ | ||
| (CZ(0, 1), [CZ(0, 1)]), | ||
| (CZ(1, 0), [CZ(1, 0)]), | ||
| ], | ||
| ids=["CZ_0_1", "CZ_1_0"], | ||
| ) | ||
| def test_decomposes_CZ(decomposer: Can2CZDecomposer, gate: Gate, expected_result: list[Gate]) -> None: # noqa: N802 | ||
| decomposed_gate = decomposer.decompose(gate) | ||
| check_gate_decomposition(gate, decomposed_gate) | ||
| assert decomposed_gate == expected_result | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("gate", "expected_result"), | ||
| [ | ||
| (CNOT(0, 1), [Ry(1, -pi / 2), CZ(0, 1), Ry(1, pi / 2)]), | ||
| (CNOT(1, 0), [Ry(0, -pi / 2), CZ(1, 0), Ry(0, pi / 2)]), | ||
| ], | ||
| ids=["CNOT_0_1", "CNOT_1_0"], | ||
| ) | ||
| def test_decomposes_CNOT(decomposer: Can2CZDecomposer, gate: Gate, expected_result: list[Gate]) -> None: # noqa: N802 | ||
| decomposed_gate = decomposer.decompose(gate) | ||
| check_gate_decomposition(gate, decomposed_gate) | ||
| assert decomposed_gate == expected_result | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "gate", | ||
| [ | ||
| CRk(0, 1, 2), | ||
| CRk(1, 0, 2), | ||
| CR(0, 1, pi / 3), | ||
| CR(1, 0, pi / 3), | ||
| SWAP(0, 1), | ||
| SWAP(1, 0), | ||
| ], | ||
| ids=["CRk_0_1_2", "CRk_1_0_2", "CR_0_1_pi_3", "CR_1_0_pi_3", "SWAP_0_1", "SWAP_1_0"], | ||
| ) | ||
| def test_decomposes_known_two_qubit_gates(decomposer: Can2CZDecomposer, gate: Gate) -> None: | ||
| decomposed_gate = decomposer.decompose(gate) | ||
| check_gate_decomposition(gate, decomposed_gate) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "gate", | ||
| [ | ||
| TwoQubitGate( | ||
| qubit0=1, | ||
| qubit1=0, | ||
| gate_semantic=CanonicalGateSemantic( | ||
| axis=(0.3, 0.2, 0.1), | ||
| rotations=[ | ||
| BlochSphereRotation((0, 1, 0), 0.4 * pi, 0.2 * pi), | ||
| BlochSphereRotation((1, 0, 0), 0.5 * pi, 0.25 * pi), | ||
| BlochSphereRotation((1, 0, 0), 0.2 * pi, 0.1 * pi), | ||
| BlochSphereRotation((0, 0, 1), 0.3 * pi, 0.15 * pi), | ||
| ], | ||
| ), | ||
| ), | ||
| TwoQubitGate( | ||
| qubit0=0, | ||
| qubit1=1, | ||
| gate_semantic=ControlledGateSemantic(target_bsr=BlochSphereRotation((0, 1, 0), pi / 5, pi / 10)), | ||
| ), | ||
| TwoQubitGate( | ||
| qubit0=0, | ||
| qubit1=1, | ||
| gate_semantic=MatrixGateSemantic( | ||
| matrix=(1 / 2) | ||
| * np.array( | ||
| [ | ||
| [1, 1, 1, 1], | ||
| [1, -1, 1, -1], | ||
| [1, 1, -1, -1], | ||
| [1, -1, -1, 1], | ||
| ], | ||
| dtype=np.complex128, | ||
| ) | ||
| ), | ||
| ), | ||
| ], | ||
| ids=["canonical", "controlled", "matrix"], | ||
| ) | ||
| def test_decomposes_other_two_qubit_gate_semantics(decomposer: Can2CZDecomposer, gate: Gate) -> None: | ||
| decomposed_gate = decomposer.decompose(gate) | ||
| check_gate_decomposition(gate, decomposed_gate) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.