From cd9fac9050fae2bc2b3eebca61fe0fd3e66e4e10 Mon Sep 17 00:00:00 2001 From: Pietropaolo Frisoni Date: Thu, 31 Oct 2024 13:12:12 -0400 Subject: [PATCH] Adding string representation for some operators (#6493) **Context:** The string representation format of some operators is slightly different from others. This is because some do not have their own string representation. Furthermore, the output of the Hadamard operator should be updated since its string representation recently changed. **Description of the Change:** - Change the output in the doc of the `qml.Hadamard` operator, since its string representation changed during this release - Add a string representation for the `qml.S` operator and change the output in the doc - Add a string representation for the `qml.T` operator and change the output in the doc - Add a string representation for the `qml.SX` operator and change the output in the doc **Benefits:** Correct doc output and more consistency among string representation for non-parametrized single wire operators. **Possible Drawbacks:** None that I can think of. This may cause failures in some plugin tests that check explicitly the string representation of such operators, but this failure should be immediate to fix. **Related GitHub Issues:** None. --- doc/development/adding_operators.rst | 2 +- doc/introduction/inspecting_circuits.rst | 2 +- doc/introduction/operations.rst | 2 +- doc/releases/changelog-0.39.0.md | 3 ++ pennylane/ops/op_math/controlled.py | 2 +- pennylane/ops/op_math/controlled_ops.py | 38 +++++++-------- pennylane/ops/op_math/linear_combination.py | 4 +- pennylane/ops/op_math/pow.py | 4 +- pennylane/ops/qubit/hamiltonian.py | 4 +- pennylane/ops/qubit/non_parametric_ops.py | 47 ++++++++++++++----- .../ops/qubit/parametric_ops_multi_qubit.py | 6 +-- .../ops/qubit/parametric_ops_single_qubit.py | 2 +- pennylane/ops/qubit/qchem_ops.py | 44 ++++++++--------- pennylane/queuing.py | 2 +- pennylane/tape/qscript.py | 12 ++--- pennylane/tape/tape.py | 6 +-- pennylane/templates/embeddings/iqp.py | 12 ++--- pennylane/templates/subroutines/aqft.py | 4 +- pennylane/templates/subroutines/qft.py | 6 +-- .../transforms/convert_to_numpy_parameters.py | 2 +- pennylane/transforms/zx/converter.py | 4 +- tests/ops/op_math/test_adjoint.py | 4 +- tests/ops/op_math/test_controlled.py | 6 +-- tests/ops/op_math/test_evolution.py | 2 +- tests/ops/op_math/test_exp.py | 2 +- tests/ops/op_math/test_pow_op.py | 2 +- tests/ops/qubit/test_non_parametric_ops.py | 6 +++ 27 files changed, 130 insertions(+), 100 deletions(-) diff --git a/doc/development/adding_operators.rst b/doc/development/adding_operators.rst index f8391929c5b..c7749213632 100644 --- a/doc/development/adding_operators.rst +++ b/doc/development/adding_operators.rst @@ -79,7 +79,7 @@ The basic components of operators are the following: >>> op = qml.PauliX(0) >>> op.diagonalizing_gates() - [Hadamard(wires=[0])] + [H(0)] >>> op.eigvals() [ 1 -1] diff --git a/doc/introduction/inspecting_circuits.rst b/doc/introduction/inspecting_circuits.rst index 5226a049651..8aabe88f0e8 100644 --- a/doc/introduction/inspecting_circuits.rst +++ b/doc/introduction/inspecting_circuits.rst @@ -331,7 +331,7 @@ Using the above example, we get: >>> for k, v in g2.adjacency(): ... print(k, v) -Hadamard(wires=[0]) {expval(Z(0)): {0: {'wire': 0}}} +H(0) {expval(Z(0)): {0: {'wire': 0}}} CNOT(wires=[1, 2]) {CNOT(wires=[2, 3]): {0: {'wire': 2}}, CNOT(wires=[3, 1]): {0: {'wire': 1}}} CNOT(wires=[2, 3]) {CNOT(wires=[3, 1]): {0: {'wire': 3}}} CNOT(wires=[3, 1]) {} diff --git a/doc/introduction/operations.rst b/doc/introduction/operations.rst index 7d223d1260d..041656f3a0f 100644 --- a/doc/introduction/operations.rst +++ b/doc/introduction/operations.rst @@ -68,7 +68,7 @@ These operator functions act on operators to produce new operators. >>> op = qml.sum(qml.Hadamard(0), op) >>> op = qml.s_prod(1.2, op) >>> op -1.2 * (Hadamard(wires=[0]) + X(0) @ Z(1)) +1.2 * (H(0) + X(0) @ Z(1)) Operator to Other functions ^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/doc/releases/changelog-0.39.0.md b/doc/releases/changelog-0.39.0.md index da0fe901fae..7e1a1ef13b6 100644 --- a/doc/releases/changelog-0.39.0.md +++ b/doc/releases/changelog-0.39.0.md @@ -192,6 +192,9 @@

Other Improvements

+* Added string representation for the `qml.S`, `qml.T`, and `qml.SX` operators. + [(#6493)](https://github.com/PennyLaneAI/pennylane/pull/6493) + * Added `qml.H` as an alias for the Hadamard operator. [(#6450)](https://github.com/PennyLaneAI/pennylane/pull/6450) diff --git a/pennylane/ops/op_math/controlled.py b/pennylane/ops/op_math/controlled.py index 6448f1dc061..48357c6abfe 100644 --- a/pennylane/ops/op_math/controlled.py +++ b/pennylane/ops/op_math/controlled.py @@ -103,7 +103,7 @@ def circuit(x): and individual :class:`~.operation.Operator`'s. >>> qml.ctrl(qml.Hadamard(0), (1,2)) - Controlled(Hadamard(wires=[0]), control_wires=[1, 2]) + Controlled(H(0), control_wires=[1, 2]) Controlled operations work with all other forms of operator math and simplification: diff --git a/pennylane/ops/op_math/controlled_ops.py b/pennylane/ops/op_math/controlled_ops.py index 3bcf4d98a0c..9dcebb6e231 100644 --- a/pennylane/ops/op_math/controlled_ops.py +++ b/pennylane/ops/op_math/controlled_ops.py @@ -384,7 +384,7 @@ def compute_decomposition(wires): # pylint: disable=arguments-differ **Example:** >>> print(qml.CY.compute_decomposition([0, 1])) - [CRY(3.141592653589793, wires=[0, 1])), S(wires=[0])] + [CRY(3.141592653589793, wires=[0, 1])), S(0)] """ return [qml.CRY(np.pi, wires=wires), qml.S(wires=wires[0])] @@ -709,20 +709,20 @@ def compute_decomposition(wires): # pylint: disable=arguments-differ >>> qml.CCZ.compute_decomposition((0,1,2)) [CNOT(wires=[1, 2]), - Adjoint(T(wires=[2])), + Adjoint(T(2)), CNOT(wires=[0, 2]), - T(wires=[2]), + T(2), CNOT(wires=[1, 2]), - Adjoint(T(wires=[2])), + Adjoint(T(2)), CNOT(wires=[0, 2]), - T(wires=[2]), - T(wires=[1]), + T(2), + T(1), CNOT(wires=[0, 1]), - Hadamard(wires=[2]), - T(wires=[0]), - Adjoint(T(wires=[1])), + H(2), + T(0), + Adjoint(T(1)), CNOT(wires=[0, 1]), - Hadamard(wires=[2])] + H(2)] """ return [ @@ -962,20 +962,20 @@ def compute_decomposition(wires): # pylint: disable=arguments-differ **Example:** >>> qml.Toffoli.compute_decomposition((0,1,2)) - [Hadamard(wires=[2]), + [H(2), CNOT(wires=[1, 2]), - Adjoint(T(wires=[2])), + Adjoint(T(2)), CNOT(wires=[0, 2]), - T(wires=[2]), + T(2), CNOT(wires=[1, 2]), - Adjoint(T(wires=[2])), + Adjoint(T(2)), CNOT(wires=[0, 2]), - T(wires=[2]), - T(wires=[1]), + T(2), + T(1), CNOT(wires=[0, 1]), - Hadamard(wires=[2]), - T(wires=[0]), - Adjoint(T(wires=[1])), + H(2), + T(0), + Adjoint(T(1)), CNOT(wires=[0, 1])] """ diff --git a/pennylane/ops/op_math/linear_combination.py b/pennylane/ops/op_math/linear_combination.py index c14975d1c3d..bbada7385f7 100644 --- a/pennylane/ops/op_math/linear_combination.py +++ b/pennylane/ops/op_math/linear_combination.py @@ -58,7 +58,7 @@ class LinearCombination(Sum): >>> obs = [qml.X(0) @ qml.Z(1), qml.Z(0) @ qml.Hadamard(2)] >>> H = qml.ops.LinearCombination(coeffs, obs) >>> print(H) - 0.2 * (X(0) @ Z(1)) + -0.543 * (Z(0) @ Hadamard(wires=[2])) + 0.2 * (X(0) @ Z(1)) + -0.543 * (Z(0) @ H(2)) The coefficients can be a trainable tensor, for example: @@ -67,7 +67,7 @@ class LinearCombination(Sum): >>> obs = [qml.X(0) @ qml.Z(1), qml.Z(0) @ qml.Hadamard(2)] >>> H = qml.ops.LinearCombination(coeffs, obs) >>> print(H) - 0.2 * (X(0) @ Z(1)) + -0.543 * (Z(0) @ Hadamard(wires=[2])) + 0.2 * (X(0) @ Z(1)) + -0.543 * (Z(0) @ H(2)) A ``LinearCombination`` can store information on which commuting observables should be measured together in diff --git a/pennylane/ops/op_math/pow.py b/pennylane/ops/op_math/pow.py index f8ad370d81f..c1eaa434102 100644 --- a/pennylane/ops/op_math/pow.py +++ b/pennylane/ops/op_math/pow.py @@ -79,7 +79,7 @@ def pow(base, z=1, lazy=True, id=None): >>> qml.pow(qml.X(0), 0.5) X(0)**0.5 >>> qml.pow(qml.X(0), 0.5, lazy=False) - SX(wires=[0]) + SX(0) >>> qml.pow(qml.X(0), 0.1, lazy=False) X(0)**0.1 >>> qml.pow(qml.X(0), 2, lazy=False) @@ -118,7 +118,7 @@ class Pow(ScalarSymbolicOp): >>> sqrt_x = Pow(qml.X(0), 0.5) >>> sqrt_x.decomposition() - [SX(wires=[0])] + [SX(0)] >>> qml.matrix(sqrt_x) array([[0.5+0.5j, 0.5-0.5j], [0.5-0.5j, 0.5+0.5j]]) diff --git a/pennylane/ops/qubit/hamiltonian.py b/pennylane/ops/qubit/hamiltonian.py index f2e2d18b86c..9c37be86146 100644 --- a/pennylane/ops/qubit/hamiltonian.py +++ b/pennylane/ops/qubit/hamiltonian.py @@ -102,7 +102,7 @@ class Hamiltonian(Observable): >>> obs = [qml.X(0) @ qml.Z(1), qml.Z(0) @ qml.Hadamard(2)] >>> H = qml.Hamiltonian(coeffs, obs) >>> print(H) - 0.2 * (X(0) @ Z(1)) + -0.543 * (Z(0) @ Hadamard(wires=[2])) + 0.2 * (X(0) @ Z(1)) + -0.543 * (Z(0) @ H(2)) The coefficients can be a trainable tensor, for example: @@ -110,7 +110,7 @@ class Hamiltonian(Observable): >>> obs = [qml.X(0) @ qml.Z(1), qml.Z(0) @ qml.Hadamard(2)] >>> H = qml.Hamiltonian(coeffs, obs) >>> print(H) - 0.2 * (X(0) @ Z(1)) + -0.543 * (Z(0) @ Hadamard(wires=[2])) + 0.2 * (X(0) @ Z(1)) + -0.543 * (Z(0) @ H(2)) A ``qml.Hamiltonian`` stores information on which commuting observables should be measured together in a circuit: diff --git a/pennylane/ops/qubit/non_parametric_ops.py b/pennylane/ops/qubit/non_parametric_ops.py index b4e0d8407dc..025a283a50b 100644 --- a/pennylane/ops/qubit/non_parametric_ops.py +++ b/pennylane/ops/qubit/non_parametric_ops.py @@ -338,7 +338,7 @@ def compute_diagonalizing_gates(wires: WiresLike) -> list[qml.operation.Operator **Example** >>> print(qml.X.compute_diagonalizing_gates(wires=[0])) - [Hadamard(wires=[0])] + [H(0)] """ return [Hadamard(wires=wires)] @@ -529,7 +529,7 @@ def compute_diagonalizing_gates(wires: WiresLike) -> list[qml.operation.Operator **Example** >>> print(qml.Y.compute_diagonalizing_gates(wires=[0])) - [Z(0), S(wires=[0]), Hadamard(wires=[0])] + [Z(0), S(0), H(0)] """ return [ Z(wires=wires), @@ -815,6 +815,13 @@ class S(Operation): batch_size = None + def __repr__(self) -> str: + """String representation.""" + wire = self.wires[0] + if isinstance(wire, str): + return f"S('{wire}')" + return f"S({wire})" + @staticmethod @lru_cache() def compute_matrix() -> np.ndarray: # pylint: disable=arguments-differ @@ -927,6 +934,13 @@ class T(Operation): batch_size = None + def __repr__(self) -> str: + """String representation.""" + wire = self.wires[0] + if isinstance(wire, str): + return f"T('{wire}')" + return f"T({wire})" + @staticmethod @lru_cache() def compute_matrix() -> np.ndarray: # pylint: disable=arguments-differ @@ -1037,6 +1051,13 @@ class SX(Operation): basis = "X" + def __repr__(self) -> str: + """String representation.""" + wire = self.wires[0] + if isinstance(wire, str): + return f"SX('{wire}')" + return f"SX({wire})" + @staticmethod @lru_cache() def compute_matrix() -> np.ndarray: # pylint: disable=arguments-differ @@ -1322,7 +1343,7 @@ def compute_decomposition(wires: WiresLike) -> list[qml.operation.Operator]: [Z(0), CNOT(wires=[0, 1]), - SX(wires=[1]), + SX(1), RX(1.5707963267948966, wires=[0]), RY(1.5707963267948966, wires=[0]), RX(1.5707963267948966, wires=[0])] @@ -1438,12 +1459,12 @@ def compute_decomposition(wires: WiresLike) -> list[qml.operation.Operator]: **Example:** >>> print(qml.ISWAP.compute_decomposition((0,1))) - [S(wires=[0]), - S(wires=[1]), - Hadamard(wires=[0]), + [S(0), + S(1), + H(0), CNOT(wires=[0, 1]), CNOT(wires=[1, 0]), - Hadamard(wires=[1])] + H(1)] """ return [ @@ -1563,18 +1584,18 @@ def compute_decomposition(wires: WiresLike) -> list[qml.operation.Operator]: **Example:** >>> print(qml.SISWAP.compute_decomposition((0,1))) - [SX(wires=[0]), + [SX(0), RZ(1.5707963267948966, wires=[0]), CNOT(wires=[0, 1]), - SX(wires=[0]), + SX(0), RZ(5.497787143782138, wires=[0]), - SX(wires=[0]), + SX(0), RZ(1.5707963267948966, wires=[0]), - SX(wires=[1]), + SX(1), RZ(5.497787143782138, wires=[1]), CNOT(wires=[0, 1]), - SX(wires=[0]), - SX(wires=[1])] + SX(0), + SX(1)] """ return [ diff --git a/pennylane/ops/qubit/parametric_ops_multi_qubit.py b/pennylane/ops/qubit/parametric_ops_multi_qubit.py index e36cf683203..39278435ad1 100644 --- a/pennylane/ops/qubit/parametric_ops_multi_qubit.py +++ b/pennylane/ops/qubit/parametric_ops_multi_qubit.py @@ -482,10 +482,10 @@ def compute_decomposition( **Example:** >>> qml.PauliRot.compute_decomposition(1.2, "XY", wires=(0,1)) - [Hadamard(wires=[0]), + [H(0), RX(1.5707963267948966, wires=[1]), MultiRZ(1.2, wires=[0, 1]), - Hadamard(wires=[0]), + H(0), RX(-1.5707963267948966, wires=[1])] """ @@ -1286,7 +1286,7 @@ def compute_decomposition(phi: TensorLike, wires: WiresLike) -> list["qml.operat **Example:** >>> qml.IsingXY.compute_decomposition(1.23, wires=(0,1)) - [Hadamard(wires=[0]), CY(wires=[0, 1]), RY(0.615, wires=[0]), RX(-0.615, wires=[1]), CY(wires=[0, 1]), Hadamard(wires=[0])] + [H(0), CY(wires=[0, 1]), RY(0.615, wires=[0]), RX(-0.615, wires=[1]), CY(wires=[0, 1]), H(0)] """ return [ diff --git a/pennylane/ops/qubit/parametric_ops_single_qubit.py b/pennylane/ops/qubit/parametric_ops_single_qubit.py index fe02238bafd..35e2b921e0f 100644 --- a/pennylane/ops/qubit/parametric_ops_single_qubit.py +++ b/pennylane/ops/qubit/parametric_ops_single_qubit.py @@ -700,7 +700,7 @@ def simplify(self) -> "Rot": >>> qml.Rot(np.pi / 2, 0.1, -np.pi / 2, wires=0).simplify() RX(0.1, wires=[0]) >>> qml.Rot(np.pi, np.pi/2, 0, 0).simplify() - Hadamard(wires=[0]) + H(0) """ p0, p1, p2 = [p % (4 * np.pi) for p in self.data] diff --git a/pennylane/ops/qubit/qchem_ops.py b/pennylane/ops/qubit/qchem_ops.py index 65dddb82acf..a08731a2564 100644 --- a/pennylane/ops/qubit/qchem_ops.py +++ b/pennylane/ops/qubit/qchem_ops.py @@ -224,22 +224,22 @@ def compute_decomposition(phi: TensorLike, wires: WiresLike) -> list["qml.operat **Example:** >>> qml.SingleExcitation.compute_decomposition(1.23, wires=(0,1)) - [Adjoint(T(wires=[0])), - Hadamard(wires=[0]), - S(wires=[0]), - Adjoint(T(wires=[1])), - Adjoint(S(wires=[1])), - Hadamard(wires=[1]), + [Adjoint(T(0)), + H(0), + S(0), + Adjoint(T(1)), + Adjoint(S(1)), + H(1), CNOT(wires=[1, 0]), RZ(-0.615, wires=[0]), RY(0.615, wires=[1]), CNOT(wires=[1, 0]), - Adjoint(S(wires=[0])), - Hadamard(wires=[0]), - T(wires=[0]), - Hadamard(wires=[1]), - S(wires=[1]), - T(wires=[1])] + Adjoint(S(0)), + H(0), + T(0), + H(1), + S(1), + T(1)] """ # This decomposition was found by plugging the matrix representation @@ -680,14 +680,14 @@ def compute_decomposition(phi: TensorLike, wires: WiresLike) -> list["qml.operat >>> qml.DoubleExcitation.compute_decomposition(1.23, wires=(0,1,2,3)) [CNOT(wires=[2, 3]), CNOT(wires=[0, 2]), - Hadamard(wires=[3]), - Hadamard(wires=[0]), + H(3), + H(0), CNOT(wires=[2, 3]), CNOT(wires=[0, 1]), RY(0.15375, wires=[1]), RY(-0.15375, wires=[0]), CNOT(wires=[0, 3]), - Hadamard(wires=[3]), + H(3), CNOT(wires=[3, 1]), RY(0.15375, wires=[1]), RY(-0.15375, wires=[0]), @@ -696,14 +696,14 @@ def compute_decomposition(phi: TensorLike, wires: WiresLike) -> list["qml.operat RY(-0.15375, wires=[1]), RY(0.15375, wires=[0]), CNOT(wires=[3, 1]), - Hadamard(wires=[3]), + H(3), CNOT(wires=[0, 3]), RY(-0.15375, wires=[1]), RY(0.15375, wires=[0]), CNOT(wires=[0, 1]), CNOT(wires=[2, 0]), - Hadamard(wires=[0]), - Hadamard(wires=[3]), + H(0), + H(3), CNOT(wires=[0, 2]), CNOT(wires=[2, 3])] @@ -1268,11 +1268,11 @@ def compute_decomposition(phi: TensorLike, wires: WiresLike) -> list["qml.operat **Example:** >>> qml.FermionicSWAP.compute_decomposition(0.2, wires=(0, 1)) - [Hadamard(wires=[0]), - Hadamard(wires=[1]), + [H(0), + H(1), MultiRZ(0.1, wires=[0, 1]), - Hadamard(wires=[0]), - Hadamard(wires=[1]), + H(0), + H(1), RX(1.5707963267948966, wires=[0]), RX(1.5707963267948966, wires=[1]), MultiRZ(0.1, wires=[0, 1]), diff --git a/pennylane/queuing.py b/pennylane/queuing.py index ed6b7e0d501..41c6ed450fa 100644 --- a/pennylane/queuing.py +++ b/pennylane/queuing.py @@ -528,7 +528,7 @@ def circuit(x): qml.CNOT(wires=[0, 1]) >>> tape1.operations - [Hadamard(wires=[1]), , X(0), CNOT(wires=[0, 1])] + [H(1), , X(0), CNOT(wires=[0, 1])] >>> tape2.operations [X(0), RZ(0.2, wires=[0])] """ diff --git a/pennylane/tape/qscript.py b/pennylane/tape/qscript.py index f4c976cf165..4ff963d1fd4 100644 --- a/pennylane/tape/qscript.py +++ b/pennylane/tape/qscript.py @@ -343,13 +343,13 @@ def diagonalizing_gates(self) -> list[Operation]: >>> tape = qml.tape.QuantumScript([], [qml.expval(X(0))]) >>> tape.diagonalizing_gates - [Hadamard(wires=[0])] + [H(0)] If the tape includes multiple observables, they are each diagonalized individually: >>> tape = qml.tape.QuantumScript([], [qml.expval(X(0)), qml.var(Y(1))]) >>> tape.diagonalizing_gates - [Hadamard(wires=[0]), Z(1), S(wires=[1]), Hadamard(wires=[1])] + [H(0), Z(1), S(1), H(1)] .. warning:: If the tape contains multiple observables acting on the same wire, @@ -360,7 +360,7 @@ def diagonalizing_gates(self) -> list[Operation]: >>> tape = qml.tape.QuantumScript([], [qml.expval(X(0)), qml.var(Y(0))]) >>> tape.diagonalizing_gates - [Hadamard(wires=[0]), Z(0), S(wires=[0]), Hadamard(wires=[0])] + [H(0), Z(0), S(0), H(0)] If it is relevant for your application, applying :func:`~.pennylane.transforms.split_non_commuting` to a tape will split it into multiple @@ -370,7 +370,7 @@ def diagonalizing_gates(self) -> list[Operation]: >>> tape = qml.tape.QuantumScript([], [qml.expval(X(0)+Y(1))]) >>> tape.diagonalizing_gates - [Hadamard(wires=[0]), Z(1), S(wires=[1]), Hadamard(wires=[1])] + [H(0), Z(1), S(1), H(1)] However, for operators that contain multiple terms on the same wire, a single diagonalizing operator will be returned that diagonalizes the full operator as a unit: @@ -1080,7 +1080,7 @@ def expand( >>> tape = qml.tape.QuantumScript([], [qml.expval(qml.X(0))]) >>> tape.expand(expand_measurements=True).circuit - [Hadamard(wires=[0]), expval(eigvals=[ 1. -1.], wires=[0])] + [H(0), expval(eigvals=[ 1. -1.], wires=[0])] """ return qml.tape.tape.expand_tape( @@ -1399,7 +1399,7 @@ def qfunc(x): ... _ = qml.RY(1.0, wires=0) ... qs = make_qscript(qfunc)(0.5) >>> qs.operations - [Hadamard(wires=[0]), CNOT(wires=[0, 1]), RX(0.5, wires=[0])] + [H(0), CNOT(wires=[0, 1]), RX(0.5, wires=[0])] Note that the currently recording queue did not queue any of these quantum operations: diff --git a/pennylane/tape/tape.py b/pennylane/tape/tape.py index 414b93e26d3..3c3e21caabb 100644 --- a/pennylane/tape/tape.py +++ b/pennylane/tape/tape.py @@ -228,7 +228,7 @@ def expand_tape(tape, depth=1, stop_at=None, expand_measurements=False): >>> tape = qml.tape.QuantumScript([], [qml.expval(qml.X(0))]) >>> expand_tape(tape, expand_measurements=True).circuit - [Hadamard(wires=[0]), expval(eigvals=[ 1. -1.], wires=[0])] + [H(0), expval(eigvals=[ 1. -1.], wires=[0])] """ @@ -372,11 +372,11 @@ class QuantumTape(QuantumScript, AnnotatedQueue): Tapes can be constructed by directly providing operations and measurements: - >>> ops = [qml.BasisState([1,0], wires=0), qml.S(0), qml.T(1)] + >>> ops = [qml.BasisState([1, 0], wires=[0, 1]), qml.S(0), qml.T(1)] >>> measurements = [qml.state()] >>> tape = qml.tape.QuantumTape(ops, measurements) >>> tape.circuit - [BasisState([1, 0], wires=[0]), S(wires=[0]), T(wires=[1]), state(wires=[])] + [BasisState(array([1, 0]), wires=[0, 1]), S(0), T(1), state(wires=[])] They can also be populated into a recording tape via queuing. diff --git a/pennylane/templates/embeddings/iqp.py b/pennylane/templates/embeddings/iqp.py index 23ebc88b8d1..1a9eb46a6b7 100644 --- a/pennylane/templates/embeddings/iqp.py +++ b/pennylane/templates/embeddings/iqp.py @@ -229,13 +229,13 @@ def compute_decomposition( >>> features = torch.tensor([1., 2., 3.]) >>> pattern = [(0, 1), (0, 2), (1, 2)] >>> qml.IQPEmbedding.compute_decomposition(features, wires=[0, 1, 2], n_repeats=2, pattern=pattern) - [Hadamard(wires=[0]), RZ(tensor(1.), wires=[0]), - Hadamard(wires=[1]), RZ(tensor(2.), wires=[1]), - Hadamard(wires=[2]), RZ(tensor(3.), wires=[2]), + [H(0), RZ(tensor(1.), wires=[0]), + H(1), RZ(tensor(2.), wires=[1]), + H(2), RZ(tensor(3.), wires=[2]), MultiRZ(tensor(2.), wires=[0, 1]), MultiRZ(tensor(3.), wires=[0, 2]), MultiRZ(tensor(6.), wires=[1, 2]), - Hadamard(wires=[0]), RZ(tensor(1.), wires=[0]), - Hadamard(wires=[1]), RZ(tensor(2.), wires=[1]), - Hadamard(wires=[2]), RZ(tensor(3.), wires=[2]), + H(0), RZ(tensor(1.), wires=[0]), + H(1), RZ(tensor(2.), wires=[1]), + H(2), RZ(tensor(3.), wires=[2]), MultiRZ(tensor(2.), wires=[0, 1]), MultiRZ(tensor(3.), wires=[0, 2]), MultiRZ(tensor(6.), wires=[1, 2])] """ wires = qml.wires.Wires(wires) diff --git a/pennylane/templates/subroutines/aqft.py b/pennylane/templates/subroutines/aqft.py index 04144ca15f6..534e058611c 100644 --- a/pennylane/templates/subroutines/aqft.py +++ b/pennylane/templates/subroutines/aqft.py @@ -160,8 +160,8 @@ def compute_decomposition(wires, order): # pylint: disable=arguments-differ **Example:** - >>> qml.AQFT.compute_decomposition((0, 1, 2), 3, order=1) - [Hadamard(wires=[0]), ControlledPhaseShift(1.5707963267948966, wires=[1, 0]), Hadamard(wires=[1]), ControlledPhaseShift(1.5707963267948966, wires=[2, 1]), Hadamard(wires=[2]), SWAP(wires=[0, 2])] + >>> qml.AQFT.compute_decomposition((0, 1, 2), order=1) + [H(0), ControlledPhaseShift(1.5707963267948966, wires=[1, 0]), H(1), ControlledPhaseShift(1.5707963267948966, wires=[2, 1]), H(2), SWAP(wires=[0, 2])] """ n_wires = len(wires) diff --git a/pennylane/templates/subroutines/qft.py b/pennylane/templates/subroutines/qft.py index be2a1057a1d..2b3a4e7464f 100644 --- a/pennylane/templates/subroutines/qft.py +++ b/pennylane/templates/subroutines/qft.py @@ -167,9 +167,9 @@ def compute_decomposition(wires, n_wires): # pylint: disable=arguments-differ,u **Example:** >>> qml.QFT.compute_decomposition(wires=(0,1,2), n_wires=3) - [Hadamard(wires=[0]), ControlledPhaseShift(1.5707963267948966, wires=Wires([1, 0])), - ControlledPhaseShift(0.7853981633974483, wires=Wires([2, 0])), Hadamard(wires=[1]), - ControlledPhaseShift(1.5707963267948966, wires=Wires([2, 1])), Hadamard(wires=[2]), + [H(0), ControlledPhaseShift(1.5707963267948966, wires=Wires([1, 0])), + ControlledPhaseShift(0.7853981633974483, wires=Wires([2, 0])), H(1), + ControlledPhaseShift(1.5707963267948966, wires=Wires([2, 1])), H(2), SWAP(wires=[0, 2])] """ diff --git a/pennylane/transforms/convert_to_numpy_parameters.py b/pennylane/transforms/convert_to_numpy_parameters.py index bd5c6084c8c..ca04218f711 100644 --- a/pennylane/transforms/convert_to_numpy_parameters.py +++ b/pennylane/transforms/convert_to_numpy_parameters.py @@ -64,7 +64,7 @@ def convert_to_numpy_parameters(tape: QuantumScript) -> tuple[QuantumScriptBatch >>> circuit = qml.tape.QuantumScript(ops, measurements) >>> [new_circuit], _ = convert_to_numpy_parameters(circuit) >>> new_circuit.circuit - [S(wires=[0]), + [S(0), RX(0.1234000027179718, wires=[0]), state(wires=[]), expval(Hermitian(array([[1., 0.], diff --git a/pennylane/transforms/zx/converter.py b/pennylane/transforms/zx/converter.py index 0d91215d847..701cad614ee 100644 --- a/pennylane/transforms/zx/converter.py +++ b/pennylane/transforms/zx/converter.py @@ -428,10 +428,10 @@ def circuit(p): >>> pennylane_tape.operations [Z(0), - T(wires=[0]), + T(0), RX(0.1, wires=[1]), Z(0), - Adjoint(T(wires=[0])), + Adjoint(T(0)), Z(1), RZ(0.3, wires=[0]), X(0), diff --git a/tests/ops/op_math/test_adjoint.py b/tests/ops/op_math/test_adjoint.py index da4a446098c..321e275ada8 100644 --- a/tests/ops/op_math/test_adjoint.py +++ b/tests/ops/op_math/test_adjoint.py @@ -424,11 +424,11 @@ class TestMiscMethods: def test_repr(self): """Test __repr__ method.""" - assert repr(Adjoint(qml.S(0))) == "Adjoint(S(wires=[0]))" + assert repr(Adjoint(qml.S(0))) == "Adjoint(S(0))" base = qml.S(0) + qml.T(0) op = Adjoint(base) - assert repr(op) == "Adjoint(S(wires=[0]) + T(wires=[0]))" + assert repr(op) == "Adjoint(S(0) + T(0))" def test_label(self): """Test that the label method for the adjoint class adds a † to the end.""" diff --git a/tests/ops/op_math/test_controlled.py b/tests/ops/op_math/test_controlled.py index 1cc3c5241f9..fc5626c2feb 100644 --- a/tests/ops/op_math/test_controlled.py +++ b/tests/ops/op_math/test_controlled.py @@ -369,16 +369,16 @@ class TestControlledMiscMethods: def test_repr(self): """Test __repr__ method.""" - assert repr(Controlled(qml.S(0), [1])) == "Controlled(S(wires=[0]), control_wires=[1])" + assert repr(Controlled(qml.S(0), [1])) == "Controlled(S(0), control_wires=[1])" base = qml.S(0) + qml.T(1) op = Controlled(base, [2]) - assert repr(op) == "Controlled(S(wires=[0]) + T(wires=[1]), control_wires=[2])" + assert repr(op) == "Controlled(S(0) + T(1), control_wires=[2])" op = Controlled(base, [2, 3], control_values=[True, False], work_wires=[4]) assert ( repr(op) - == "Controlled(S(wires=[0]) + T(wires=[1]), control_wires=[2, 3], work_wires=[4], control_values=[True, False])" + == "Controlled(S(0) + T(1), control_wires=[2, 3], work_wires=[4], control_values=[True, False])" ) def test_flatten_unflatten(self): diff --git a/tests/ops/op_math/test_evolution.py b/tests/ops/op_math/test_evolution.py index 05783ea5807..1c3c8b1e0ad 100644 --- a/tests/ops/op_math/test_evolution.py +++ b/tests/ops/op_math/test_evolution.py @@ -123,7 +123,7 @@ def test_repr_deep_operator(self): base = qml.S(0) @ qml.X(0) op = Evolution(base, 3) - assert repr(op) == "Evolution(-3j S(wires=[0]) @ X(0))" + assert repr(op) == "Evolution(-3j S(0) @ X(0))" @pytest.mark.parametrize( "op,decimals,expected", diff --git a/tests/ops/op_math/test_exp.py b/tests/ops/op_math/test_exp.py index 22c0c362705..b1dc044e3b0 100644 --- a/tests/ops/op_math/test_exp.py +++ b/tests/ops/op_math/test_exp.py @@ -680,7 +680,7 @@ def test_repr_deep_operator(self): base = qml.S(0) @ qml.PauliX(0) op = qml.ops.Exp(base, 3) # pylint:disable=no-member - assert repr(op) == "Exp(3 S(wires=[0]) @ X(0))" + assert repr(op) == "Exp(3 S(0) @ X(0))" def test_diagonalizing_gates(self): """Test that the diagonalizing gates are the same as the base diagonalizing gates.""" diff --git a/tests/ops/op_math/test_pow_op.py b/tests/ops/op_math/test_pow_op.py index b323744c56f..9e6a85d5b34 100644 --- a/tests/ops/op_math/test_pow_op.py +++ b/tests/ops/op_math/test_pow_op.py @@ -587,7 +587,7 @@ def test_repr(self): base = qml.RX(1, 0) + qml.S(1) op = Pow(base, 2.5) - assert repr(op) == "(RX(1, wires=[0]) + S(wires=[1]))**2.5" + assert repr(op) == "(RX(1, wires=[0]) + S(1))**2.5" # pylint: disable=protected-access def test_flatten_unflatten(self): diff --git a/tests/ops/qubit/test_non_parametric_ops.py b/tests/ops/qubit/test_non_parametric_ops.py index c82b6dba8f4..198ac32891f 100644 --- a/tests/ops/qubit/test_non_parametric_ops.py +++ b/tests/ops/qubit/test_non_parametric_ops.py @@ -83,10 +83,16 @@ (qml.X("a"), "X('a')"), (qml.Y("a"), "Y('a')"), (qml.Z("a"), "Z('a')"), + (qml.T("a"), "T('a')"), + (qml.S("a"), "S('a')"), + (qml.SX("a"), "SX('a')"), (qml.H(0), "H(0)"), (qml.X(1), "X(1)"), (qml.Y(2), "Y(2)"), (qml.Z(3), "Z(3)"), + (qml.T(0), "T(0)"), + (qml.S(0), "S(0)"), + (qml.SX(0), "SX(0)"), )