-
Notifications
You must be signed in to change notification settings - Fork 715
Add decompositions of RX, RY and RZ into one of the other two + Cliffords
#8569
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
base: master
Are you sure you want to change the base?
Changes from 3 commits
cf9aaff
17be12c
b1e3bf9
7339ee6
0a969e7
905c19a
b97c567
56c85f9
ca001ac
a4b23a7
2819348
d9894ce
0537964
b01be9a
d151ac6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,7 +23,13 @@ | |
| import scipy as sp | ||
|
|
||
| import pennylane as qml | ||
| from pennylane.decomposition import add_decomps, register_resources, resource_rep | ||
| from pennylane.decomposition import ( | ||
| add_decomps, | ||
| adjoint_resource_rep, | ||
| change_op_basis_resource_rep, | ||
| register_resources, | ||
| resource_rep, | ||
| ) | ||
| from pennylane.decomposition.symbolic_decomposition import ( | ||
| adjoint_rotation, | ||
| flip_zero_control, | ||
|
|
@@ -180,7 +186,25 @@ def _rx_to_rz_ry(phi, wires: WiresLike, **__): | |
| qml.RZ(-np.pi / 2, wires=wires) | ||
|
|
||
|
|
||
| add_decomps(RX, _rx_to_rot, _rx_to_rz_ry) | ||
| def _rx_to_ry_cliff_resources(): | ||
| return {change_op_basis_resource_rep(qml.S, qml.RY): 1} | ||
|
|
||
|
|
||
| @register_resources(_rx_to_ry_cliff_resources) | ||
| def _rx_to_ry_cliff(phi, wires: WiresLike, **__): | ||
| qml.change_op_basis(qml.S(wires), qml.RY(phi, wires)) | ||
|
|
||
|
|
||
| def _rx_to_rz_cliff_resources(): | ||
| return {change_op_basis_resource_rep(qml.Hadamard, qml.RZ, qml.Hadamard): 1} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know Hadamard transforms between X and Z bases. |
||
|
|
||
|
|
||
| @register_resources(_rx_to_rz_cliff_resources) | ||
| def _rx_to_rz_cliff(phi, wires: WiresLike, **__): | ||
| qml.change_op_basis(qml.Hadamard(wires), qml.RZ(phi, wires), qml.Hadamard(wires)) | ||
|
|
||
|
|
||
| add_decomps(RX, _rx_to_rot, _rx_to_rz_ry, _rx_to_ry_cliff, _rx_to_rz_cliff) | ||
| add_decomps("Adjoint(RX)", adjoint_rotation) | ||
| add_decomps("Pow(RX)", pow_rotation) | ||
|
|
||
|
|
@@ -349,7 +373,41 @@ def _ry_to_rz_rx(phi, wires: WiresLike, **__): | |
| qml.RZ(np.pi / 2, wires=wires) | ||
|
|
||
|
|
||
| add_decomps(RY, _ry_to_rot, _ry_to_rz_rx) | ||
| def _ry_to_rx_cliff_resources(): | ||
| return {change_op_basis_resource_rep(adjoint_resource_rep(qml.S), qml.RX, qml.S): 1} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one checks out!
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (checked this one by hand...)
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
|
|
||
| @register_resources(_ry_to_rx_cliff_resources) | ||
| def _ry_to_rx_cliff(phi, wires: WiresLike, **__): | ||
| qml.change_op_basis(qml.adjoint(qml.S(wires)), qml.RX(phi, wires), qml.S(wires)) | ||
|
|
||
|
|
||
| def _ry_to_rz_cliff_resources(): | ||
| return { | ||
| change_op_basis_resource_rep( | ||
| resource_rep( | ||
| qml.ops.op_math.Prod, | ||
| resources={adjoint_resource_rep(qml.S): 1, resource_rep(qml.Hadamard): 1}, | ||
| ), | ||
| qml.RZ, | ||
| resource_rep( | ||
| qml.ops.op_math.Prod, | ||
| resources={resource_rep(qml.S): 1, resource_rep(qml.Hadamard): 1}, | ||
| ), | ||
| ): 1 | ||
| } | ||
|
|
||
|
|
||
| @register_resources(_ry_to_rz_cliff_resources) | ||
| def _ry_to_rz_cliff(phi, wires: WiresLike, **__): | ||
| qml.change_op_basis( | ||
| qml.Hadamard(wires) @ qml.adjoint(qml.S(wires)), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one checks out!
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| qml.RZ(phi, wires), | ||
| qml.S(wires) @ qml.Hadamard(wires), | ||
| ) | ||
|
|
||
|
|
||
| add_decomps(RY, _ry_to_rot, _ry_to_rz_rx, _ry_to_rx_cliff, _ry_to_rz_cliff) | ||
| add_decomps("Adjoint(RY)", adjoint_rotation) | ||
| add_decomps("Pow(RY)", pow_rotation) | ||
|
|
||
|
|
@@ -556,7 +614,41 @@ def _rz_to_ry_rx(phi, wires: WiresLike, **__): | |
| qml.RY(-np.pi / 2, wires=wires) | ||
|
|
||
|
|
||
| add_decomps(RZ, _rz_to_rot, _rz_to_ry_rx) | ||
| def _rz_to_rx_cliff_resources(): | ||
| return {change_op_basis_resource_rep(qml.Hadamard, qml.RX, qml.Hadamard): 1} | ||
|
|
||
|
|
||
| @register_resources(_rz_to_rx_cliff_resources) | ||
| def _rz_to_rx_cliff(phi, wires: WiresLike, **__): | ||
| qml.change_op_basis(qml.Hadamard(wires), qml.RX(phi, wires), qml.Hadamard(wires)) | ||
|
|
||
|
|
||
| def _rz_to_ry_cliff_resources(): | ||
| return { | ||
| change_op_basis_resource_rep( | ||
| resource_rep( | ||
| qml.ops.op_math.Prod, | ||
| resources={resource_rep(qml.S): 1, resource_rep(qml.Hadamard): 1}, | ||
| ), | ||
| qml.RY, | ||
| resource_rep( | ||
| qml.ops.op_math.Prod, | ||
| resources={adjoint_resource_rep(qml.S): 1, resource_rep(qml.Hadamard): 1}, | ||
| ), | ||
| ): 1 | ||
| } | ||
|
|
||
|
|
||
| @register_resources(_rz_to_ry_cliff_resources) | ||
| def _rz_to_ry_cliff(phi, wires: WiresLike, **__): | ||
| qml.change_op_basis( | ||
| qml.S(wires) @ qml.Hadamard(wires), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Checks out. |
||
| qml.RY(phi, wires), | ||
| qml.Hadamard(wires) @ qml.adjoint(qml.S(wires)), | ||
| ) | ||
|
|
||
|
|
||
| add_decomps(RZ, _rz_to_rot, _rz_to_ry_rx, _rz_to_rx_cliff, _rz_to_ry_cliff) | ||
| add_decomps("Adjoint(RZ)", adjoint_rotation) | ||
| add_decomps("Pow(RZ)", pow_rotation) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -151,7 +151,24 @@ def test_validity(self, op): | |
| ) | ||
|
|
||
|
|
||
| SKIP_ASSERT_VALID = { | ||
| qml.GlobalPhase: True, | ||
| qml.QubitUnitary: {"skip_differentiation": True}, | ||
| qml.DiagonalQubitUnitary: {"skip_differentiation": True}, | ||
| qml.ControlledQubitUnitary: {"skip_differentiation": True}, | ||
| } | ||
|
|
||
|
|
||
| class TestOperations: | ||
|
|
||
| @pytest.mark.parametrize("op", ALL_OPERATIONS) | ||
| def test_assert_valid(self, op): | ||
| kwargs = SKIP_ASSERT_VALID.get(type(op), {}) | ||
| if kwargs is True: | ||
| pytest.skip() | ||
|
|
||
| qml.ops.functions.assert_valid(op, **kwargs) | ||
|
Comment on lines
+164
to
+170
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for adding this! |
||
|
|
||
| @pytest.mark.parametrize("op", ALL_OPERATIONS + BROADCASTED_OPERATIONS) | ||
| def test_parametrized_op_copy(self, op, tol): | ||
| """Tests that copied parametrized ops function as expected""" | ||
|
|
||

Uh oh!
There was an error while loading. Please reload this page.