diff --git a/tests/circuit_graph/test_circuit_graph.py b/tests/circuit_graph/test_circuit_graph.py index 4bba3b2933c..d28dc2341df 100644 --- a/tests/circuit_graph/test_circuit_graph.py +++ b/tests/circuit_graph/test_circuit_graph.py @@ -18,7 +18,6 @@ import contextlib import io -import warnings import numpy as np import pytest @@ -30,13 +29,6 @@ from pennylane.wires import Wires -@pytest.fixture(autouse=True) -def suppress_tape_property_deprecation_warning(): - warnings.filterwarnings( - "ignore", "The tape/qtape property is deprecated", category=qml.PennyLaneDeprecationWarning - ) - - @pytest.fixture(name="ops") def ops_fixture(): """A fixture of a complex example of operations that depend on previous operations.""" @@ -261,8 +253,10 @@ def test_layers(self, parametrized_circuit_gaussian, wires): dev = qml.device("default.gaussian", wires=wires) qnode = qml.QNode(parametrized_circuit_gaussian, dev) - qnode(*pnp.array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6], requires_grad=True)) - circuit = qnode.qtape.graph + tape = qml.workflow.construct_tape(qnode)( + *pnp.array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6], requires_grad=True) + ) + circuit = tape.graph layers = circuit.parametrized_layers ops = circuit.operations @@ -280,8 +274,10 @@ def test_iterate_layers(self, parametrized_circuit_gaussian, wires): dev = qml.device("default.gaussian", wires=wires) qnode = qml.QNode(parametrized_circuit_gaussian, dev) - qnode(*pnp.array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6], requires_grad=True)) - circuit = qnode.qtape.graph + tape = qml.workflow.construct_tape(qnode)( + *pnp.array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6], requires_grad=True) + ) + circuit = tape.graph result = list(circuit.iterate_parametrized_layers()) assert len(result) == 3 @@ -314,8 +310,8 @@ def test_max_simultaneous_measurements(self, circ, expected): dev = qml.device("default.qubit", wires=3) qnode = qml.QNode(circ, dev) - qnode() - circuit = qnode.qtape.graph + tape = qml.workflow.construct_tape(qnode)() + circuit = tape.graph assert circuit.max_simultaneous_measurements == expected def test_print_contents(self): diff --git a/tests/circuit_graph/test_circuit_graph_hash.py b/tests/circuit_graph/test_circuit_graph_hash.py index fc06c899795..00887af5123 100644 --- a/tests/circuit_graph/test_circuit_graph_hash.py +++ b/tests/circuit_graph/test_circuit_graph_hash.py @@ -12,9 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. """ -Unit and integration tests for creating the :mod:`pennylane` :attr:`QNode.qtape.graph.hash` attribute. +Unit and integration tests for creating the :mod:`pennylane` :attr:`tape.graph.hash` attribute. """ -import warnings import numpy as np import pytest @@ -24,13 +23,6 @@ from pennylane.wires import Wires -@pytest.fixture(autouse=True) -def suppress_tape_property_deprecation_warning(): - warnings.filterwarnings( - "ignore", "The tape/qtape property is deprecated", category=qml.PennyLaneDeprecationWarning - ) - - class TestCircuitGraphHash: """Test the creation of a hash on a CircuitGraph""" @@ -97,8 +89,8 @@ def circuit1(): return obs(op) node1 = qml.QNode(circuit1, dev) - node1.construct([], {}) - circuit_hash_1 = node1.qtape.graph.serialize() + tape = qml.workflow.construct_tape(node1)() + circuit_hash_1 = tape.graph.serialize() assert circuit_hash_1 == expected_string @@ -119,8 +111,8 @@ def circuit1(): return obs(wires=0) node1 = qml.QNode(circuit1, dev) - node1.construct([], {}) - circuit_hash_1 = node1.qtape.graph.serialize() + tape = qml.workflow.construct_tape(node1)() + circuit_hash_1 = tape.graph.serialize() assert circuit_hash_1 == expected_string @@ -140,9 +132,9 @@ def circuit1(): return obs() node1 = qml.QNode(circuit1, dev) - node1.construct([], {}) - circuit_hash_1 = node1.qtape.graph.serialize() + tape = qml.workflow.construct_tape(node1)() + circuit_hash_1 = tape.graph.serialize() assert circuit_hash_1 == expected_string @@ -161,9 +153,9 @@ def circuit1(): return obs(wires=[0, 1]) node1 = qml.QNode(circuit1, dev) - node1.construct([], {}) - circuit_hash_1 = node1.qtape.graph.serialize() + tape = qml.workflow.construct_tape(node1)() + circuit_hash_1 = tape.graph.serialize() assert circuit_hash_1 == expected_string @@ -185,8 +177,8 @@ def circuit1(): return qml.expval(qml.PauliZ(0)) node1 = qml.QNode(circuit1, dev) - node1.construct([], {}) - circuit_hash_1 = node1.qtape.graph.hash + tape = qml.workflow.construct_tape(node1)() + circuit_hash_1 = tape.graph.hash def circuit2(): qml.RX(a, wires=[0]) @@ -195,8 +187,8 @@ def circuit2(): return qml.expval(qml.PauliZ(0)) node2 = qml.QNode(circuit2, dev) - node2.construct([], {}) - circuit_hash_2 = node2.qtape.graph.hash + tape = qml.workflow.construct_tape(node2)() + circuit_hash_2 = tape.graph.hash assert circuit_hash_1 == circuit_hash_2 @@ -215,8 +207,8 @@ def circuit1(x, y): return qml.expval(qml.PauliZ(0)) node1 = qml.QNode(circuit1, dev) - node1(x, y) - circuit_hash_1 = node1.qtape.graph.hash + tape = qml.workflow.construct_tape(node1)(x, y) + circuit_hash_1 = tape.graph.hash def circuit2(x, y): qml.RX(x, wires=[0]) @@ -225,8 +217,8 @@ def circuit2(x, y): return qml.expval(qml.PauliZ(0)) node2 = qml.QNode(circuit2, dev) - node2(x, y) - circuit_hash_2 = node2.qtape.graph.hash + tape = qml.workflow.construct_tape(node2)(x, y) + circuit_hash_2 = tape.graph.hash assert circuit_hash_1 == circuit_hash_2 @@ -246,8 +238,8 @@ def circuit1(x, y): return qml.expval(qml.PauliZ(0)) node1 = qml.QNode(circuit1, dev) - node1(x, y) - circuit_hash_1 = node1.qtape.graph.hash + tape = qml.workflow.construct_tape(node1)(x, y) + circuit_hash_1 = tape.graph.hash def circuit2(x, y): qml.RX(x, wires=[0]) @@ -257,8 +249,8 @@ def circuit2(x, y): return qml.expval(qml.PauliZ(0)) node2 = qml.QNode(circuit2, dev) - node2(x, y) - circuit_hash_2 = node2.qtape.graph.hash + tape = qml.workflow.construct_tape(node2)(x, y) + circuit_hash_2 = tape.graph.hash assert circuit_hash_1 == circuit_hash_2 @@ -279,8 +271,8 @@ def circuit1(x, y): return qml.expval(qml.PauliZ(0)) node1 = qml.QNode(circuit1, dev) - node1(x, y) - circuit_hash_1 = node1.qtape.graph.hash + tape = qml.workflow.construct_tape(node1)(x, y) + circuit_hash_1 = tape.graph.hash def circuit2(x, y): qml.RX(x, wires=[0]) @@ -290,8 +282,8 @@ def circuit2(x, y): return qml.expval(qml.PauliZ(0)) node2 = qml.QNode(circuit2, dev) - node2(x, y) - circuit_hash_2 = node2.qtape.graph.hash + tape = qml.workflow.construct_tape(node2)(x, y) + circuit_hash_2 = tape.graph.hash assert circuit_hash_1 == circuit_hash_2 @@ -310,8 +302,8 @@ def circuit1(x, y): return qml.expval(qml.PauliZ(0) @ qml.PauliX(1)) node1 = qml.QNode(circuit1, dev) - node1(x, y) - circuit_hash_1 = node1.qtape.graph.hash + tape = qml.workflow.construct_tape(node1)(x, y) + circuit_hash_1 = tape.graph.hash def circuit2(x, y): qml.Rot(x, y, 0.3, wires=[0]) @@ -319,8 +311,8 @@ def circuit2(x, y): return qml.expval(qml.PauliZ(0) @ qml.PauliX(1)) node2 = qml.QNode(circuit2, dev) - node2(x, y) - circuit_hash_2 = node2.qtape.graph.hash + tape = qml.workflow.construct_tape(node2)(x, y) + circuit_hash_2 = tape.graph.hash assert circuit_hash_1 == circuit_hash_2 @@ -338,8 +330,8 @@ def circuit1(x, y): return qml.expval(qml.PauliZ(0) @ qml.PauliX(1)) node1 = qml.QNode(circuit1, dev) - node1(x, y) - circuit_hash_1 = node1.qtape.graph.hash + tape = qml.workflow.construct_tape(node1)(x, y) + circuit_hash_1 = tape.graph.hash def circuit2(x, y): qml.Rot(x, y, 0.3, wires=[0]) @@ -347,8 +339,8 @@ def circuit2(x, y): return qml.var(qml.PauliZ(0) @ qml.PauliX(1)) node2 = qml.QNode(circuit2, dev) - node2(x, y) - circuit_hash_2 = node2.qtape.graph.hash + tape = qml.workflow.construct_tape(node2)(x, y) + circuit_hash_2 = tape.graph.hash assert circuit_hash_1 != circuit_hash_2 @@ -368,8 +360,8 @@ def circuit1(x, y): return qml.expval(qml.Hermitian(matrix, wires=[0]) @ qml.PauliX(1)) node1 = qml.QNode(circuit1, dev) - node1(x, y) - circuit_hash_1 = node1.qtape.graph.hash + tape = qml.workflow.construct_tape(node1)(x, y) + circuit_hash_1 = tape.graph.hash def circuit2(x, y): qml.Rot(x, y, 0.3, wires=[0]) @@ -377,8 +369,8 @@ def circuit2(x, y): return qml.expval(qml.Hermitian(matrix, wires=[0]) @ qml.PauliX(1)) node2 = qml.QNode(circuit2, dev) - node2(x, y) - circuit_hash_2 = node2.qtape.graph.hash + tape = qml.workflow.construct_tape(node2)(x, y) + circuit_hash_2 = tape.graph.hash assert circuit_hash_1 == circuit_hash_2 @@ -400,8 +392,8 @@ def circuit1(): return qml.expval(qml.PauliZ(0) @ qml.PauliX(1)) node1 = qml.QNode(circuit1, dev) - node1.construct([], {}) - circuit_hash_1 = node1.qtape.graph.hash + tape = qml.workflow.construct_tape(node1)() + circuit_hash_1 = tape.graph.hash c = 0.6 @@ -412,8 +404,8 @@ def circuit2(): return qml.expval(qml.PauliZ(0) @ qml.PauliX(1)) node2 = qml.QNode(circuit2, dev) - node2.construct([], {}) - circuit_hash_2 = node2.qtape.graph.hash + tape = qml.workflow.construct_tape(node2)() + circuit_hash_2 = tape.graph.hash assert circuit_hash_1 != circuit_hash_2 @@ -428,16 +420,16 @@ def circuit1(): return qml.expval(qml.PauliZ(0)) node1 = qml.QNode(circuit1, dev) - node1.construct([], {}) - circuit_hash_1 = node1.qtape.graph.hash + tape = qml.workflow.construct_tape(node1)() + circuit_hash_1 = tape.graph.hash def circuit2(): qml.RY(a, wires=[0]) return qml.expval(qml.PauliZ(0)) node2 = qml.QNode(circuit2, dev) - node2.construct([], {}) - circuit_hash_2 = node2.qtape.graph.hash + tape = qml.workflow.construct_tape(node2)() + circuit_hash_2 = tape.graph.hash assert circuit_hash_1 != circuit_hash_2 @@ -458,8 +450,8 @@ def circuit1(x, y): return qml.expval(qml.PauliZ(0) @ qml.PauliX(1)) node1 = qml.QNode(circuit1, dev) - node1(x, y) - circuit_hash_1 = node1.qtape.graph.hash + tape = qml.workflow.construct_tape(node1)(x, y) + circuit_hash_1 = tape.graph.hash def circuit2(x, y): qml.RX(x, wires=[0]) @@ -469,8 +461,8 @@ def circuit2(x, y): return qml.expval(qml.PauliZ(0) @ qml.PauliX(1)) node2 = qml.QNode(circuit2, dev) - node2(x, y) - circuit_hash_2 = node2.qtape.graph.hash + tape = qml.workflow.construct_tape(node2)(x, y) + circuit_hash_2 = tape.graph.hash assert circuit_hash_1 != circuit_hash_2 @@ -490,8 +482,8 @@ def circuit1(x, y): return qml.expval(qml.PauliZ(0)) # <------------- qml.PauliZ(0) node1 = qml.QNode(circuit1, dev) - node1(x, y) - circuit_hash_1 = node1.qtape.graph.hash + tape = qml.workflow.construct_tape(node1)(x, y) + circuit_hash_1 = tape.graph.hash def circuit2(x, y): qml.RX(x, wires=[0]) @@ -503,8 +495,8 @@ def circuit2(x, y): ) # <------------- qml.PauliZ(0) @ qml.PauliX(1) node2 = qml.QNode(circuit2, dev) - node2(x, y) - circuit_hash_2 = node2.qtape.graph.hash + tape = qml.workflow.construct_tape(node2)(x, y) + circuit_hash_2 = tape.graph.hash assert circuit_hash_1 != circuit_hash_2 @@ -525,8 +517,8 @@ def circuit1(x, y): return qml.expval(qml.PauliZ(0) @ qml.PauliX(1)) node1 = qml.QNode(circuit1, dev) - node1(x, y) - circuit_hash_1 = node1.qtape.graph.hash + tape = qml.workflow.construct_tape(node1)(x, y) + circuit_hash_1 = tape.graph.hash def circuit2(x, y): qml.Rot(x, y, 0.3, wires=[0]) # <------------- x, y, 0.3 @@ -534,8 +526,8 @@ def circuit2(x, y): return qml.expval(qml.PauliZ(0) @ qml.PauliX(1)) node2 = qml.QNode(circuit2, dev) - node2(x, y) - circuit_hash_2 = node2.qtape.graph.hash + tape = qml.workflow.construct_tape(node2)(x, y) + circuit_hash_2 = tape.graph.hash assert circuit_hash_1 != circuit_hash_2 @@ -556,8 +548,8 @@ def circuit1(x, y): return qml.expval(qml.PauliZ(0) @ qml.PauliX(1)) node1 = qml.QNode(circuit1, dev) - node1(x, y) - circuit_hash_1 = node1.qtape.graph.hash + tape = qml.workflow.construct_tape(node1)(x, y) + circuit_hash_1 = tape.graph.hash def circuit2(x, y): qml.Rot(x, y, 0.5, wires=[0]) # <------------- 0.5 @@ -565,8 +557,8 @@ def circuit2(x, y): return qml.expval(qml.PauliZ(0) @ qml.PauliX(1)) node2 = qml.QNode(circuit2, dev) - node2(x, y) - circuit_hash_2 = node2.qtape.graph.hash + tape = qml.workflow.construct_tape(node2)(x, y) + circuit_hash_2 = tape.graph.hash assert circuit_hash_1 != circuit_hash_2 @@ -587,8 +579,8 @@ def circuit1(x, y): return qml.expval(qml.PauliZ(0) @ qml.PauliX(1)) node1 = qml.QNode(circuit1, dev) - node1(x, y) - circuit_hash_1 = node1.qtape.graph.hash + tape = qml.workflow.construct_tape(node1)(x, y) + circuit_hash_1 = tape.graph.hash def circuit2(x, y): qml.Rot(x, y, 0.3, wires=[0]) @@ -596,8 +588,8 @@ def circuit2(x, y): return qml.expval(qml.PauliZ(0) @ qml.PauliX(1)) node2 = qml.QNode(circuit2, dev) - node2(x, y) - circuit_hash_2 = node2.qtape.graph.hash + tape = qml.workflow.construct_tape(node2)(x, y) + circuit_hash_2 = tape.graph.hash assert circuit_hash_1 != circuit_hash_2 @@ -618,8 +610,8 @@ def circuit1(x, y): return qml.expval(qml.PauliZ(0) @ qml.PauliX(1)) # <----- (0) @ (1) node1 = qml.QNode(circuit1, dev) - node1(x, y) - circuit_hash_1 = node1.qtape.graph.hash + tape = qml.workflow.construct_tape(node1)(x, y) + circuit_hash_1 = tape.graph.hash def circuit2(x, y): qml.Rot(x, y, 0.3, wires=[0]) @@ -627,8 +619,8 @@ def circuit2(x, y): return qml.expval(qml.PauliZ(0) @ qml.PauliX(2)) # <----- (0) @ (2) node2 = qml.QNode(circuit2, dev) - node2(x, y) - circuit_hash_2 = node2.qtape.graph.hash + tape = qml.workflow.construct_tape(node2)(x, y) + circuit_hash_2 = tape.graph.hash assert circuit_hash_1 != circuit_hash_2 @@ -649,8 +641,8 @@ def circuit1(x, y): return qml.expval(qml.PauliZ(0) @ qml.PauliX(1)) node1 = qml.QNode(circuit1, dev) - node1(x, y) - circuit_hash_1 = node1.qtape.graph.hash + tape = qml.workflow.construct_tape(node1)(x, y) + circuit_hash_1 = tape.graph.hash def circuit2(x, y): qml.RX(x, wires=[0]) @@ -660,8 +652,8 @@ def circuit2(x, y): return qml.expval(qml.PauliZ(0) @ qml.PauliX(1)) node2 = qml.QNode(circuit2, dev) - node2(x, y) - circuit_hash_2 = node2.qtape.graph.hash + tape = qml.workflow.construct_tape(node2)(x, y) + circuit_hash_2 = tape.graph.hash assert circuit_hash_1 != circuit_hash_2 @@ -683,8 +675,8 @@ def circuit1(x, y): return qml.expval(qml.Hermitian(matrix_1, wires=[0]) @ qml.PauliX(1)) node1 = qml.QNode(circuit1, dev) - node1(x, y) - circuit_hash_1 = node1.qtape.graph.hash + tape = qml.workflow.construct_tape(node1)(x, y) + circuit_hash_1 = tape.graph.hash def circuit2(x, y): qml.Rot(x, y, 0.3, wires=[0]) @@ -692,7 +684,7 @@ def circuit2(x, y): return qml.expval(qml.Hermitian(matrix_2, wires=[0]) @ qml.PauliX(1)) node2 = qml.QNode(circuit2, dev) - node2(x, y) - circuit_hash_2 = node2.qtape.graph.hash + tape = qml.workflow.construct_tape(node2)(x, y) + circuit_hash_2 = tape.graph.hash assert circuit_hash_1 != circuit_hash_2 diff --git a/tests/circuit_graph/test_qasm.py b/tests/circuit_graph/test_qasm.py index 05775d4e626..cd115c3e3f3 100644 --- a/tests/circuit_graph/test_qasm.py +++ b/tests/circuit_graph/test_qasm.py @@ -14,7 +14,6 @@ """ Unit tests for the :mod:`pennylane.circuit_graph.to_openqasm()` method. """ -import warnings # pylint: disable=no-self-use,too-many-arguments,protected-access from textwrap import dedent @@ -26,13 +25,6 @@ from pennylane.wires import Wires -@pytest.fixture(autouse=True) -def suppress_tape_property_deprecation_warning(): - warnings.filterwarnings( - "ignore", "The tape/qtape property is deprecated", category=qml.PennyLaneDeprecationWarning - ) - - class TestToQasmUnitTests: """Unit tests for the to_openqasm() method""" @@ -340,18 +332,10 @@ class TestQNodeQasmIntegrationTests: when circuits are created via QNodes.""" def test_empty_circuit(self): - """Test that an empty QNode is properly + """Test that an empty tape is properly serialized into an empty QASM program.""" - dev = qml.device("default.qubit", wires=1) - - @qml.qnode(dev) - def qnode(): - return qml.expval(qml.PauliZ(0)) - - # construct the qnode circuit - qnode() - - res = qnode.qtape.to_openqasm() + tape = qml.tape.QuantumScript([], [qml.expval(qml.PauliZ(0))]) + res = tape.to_openqasm() expected = dedent( """\ OPENQASM 2.0; @@ -364,24 +348,21 @@ def qnode(): assert res == expected def test_native_qasm_gates(self): - """Test that a QNode containing solely native QASM + """Test that a tape containing solely native QASM gates is properly serialized.""" - dev = qml.device("default.qubit", wires=3) - - @qml.qnode(dev) - def qnode(): - qml.RX(0.43, wires=0) - qml.RY(0.35, wires=1) - qml.RZ(0.35, wires=2) - qml.CNOT(wires=[0, 1]) - qml.Hadamard(wires=2) - qml.CNOT(wires=[2, 0]) - qml.PauliX(wires=1) - return qml.expval(qml.PauliZ(0)) - - # construct the qnode circuit - qnode() - res = qnode.qtape.to_openqasm() + tape = qml.tape.QuantumScript( + [ + qml.RX(0.43, wires=0), + qml.RY(0.35, wires=1), + qml.RZ(0.35, wires=2), + qml.CNOT(wires=[0, 1]), + qml.Hadamard(wires=2), + qml.CNOT(wires=[2, 0]), + qml.PauliX(wires=1), + ], + [qml.expval(qml.PauliZ(0))], + ) + res = tape.to_openqasm() expected = dedent( """\ @@ -425,7 +406,7 @@ def qnode(x, y): # execute the QNode with parameters, and serialize x = np.array(0.5) y = np.array([0.2, 0.1]) - qnode(x, y) + tape = qml.workflow.construct_tape(qnode)(x, y) expected = dedent( """\ @@ -446,13 +427,13 @@ def qnode(x, y): """ ) - res = qnode.qtape.to_openqasm() + res = tape.to_openqasm() assert res == expected # execute the QNode with new parameters, and serialize again x2 = np.array(0.1) y2 = np.array([0.3, 0.2]) - qnode(x2, y2) + tape = qml.workflow.construct_tape(qnode)(x2, y2) expected = dedent( """\ @@ -473,22 +454,17 @@ def qnode(x, y): """ ) - res = qnode.qtape.to_openqasm() + res = tape.to_openqasm() assert res == expected def test_unused_wires(self): """Test that unused wires are correctly taken into account""" dev = qml.device("default.qubit", wires=5) - @qml.qnode(dev) - def qnode(): - qml.Hadamard(wires=4) - qml.CNOT(wires=[1, 0]) - return qml.expval(qml.PauliZ(0)) - - # construct the qnode circuit - qnode() - res = qnode.qtape.to_openqasm(wires=dev.wires) + tape = qml.tape.QuantumScript( + [qml.Hadamard(wires=4), qml.CNOT(wires=[1, 0])], [qml.expval(qml.PauliZ(0))] + ) + res = tape.to_openqasm(wires=dev.wires) expected = dedent( """\ @@ -513,14 +489,10 @@ def test_rotation_gate_decomposition(self): rotation gate, are correctly decomposed and serialized.""" dev = qml.device("default.qubit", wires=2) - @qml.qnode(dev) - def qnode(): - qml.Rot(0.3, 0.1, 0.2, wires=1) - return qml.expval(qml.PauliZ(0)) - - # construct the qnode circuit - qnode() - res = qnode.qtape.to_openqasm(wires=dev.wires) + tape = qml.tape.QuantumScript( + [qml.Rot(0.3, 0.1, 0.2, wires=1)], [qml.expval(qml.PauliZ(0))] + ) + res = tape.to_openqasm(wires=dev.wires) expected = dedent( """\ @@ -549,8 +521,8 @@ def qnode(state=None): return qml.expval(qml.PauliZ(0)) # construct the qnode circuit - qnode(state=np.array([1, -1, -1, 1]) / np.sqrt(4)) - res = qnode.qtape.to_openqasm(precision=11) + tape = qml.workflow.construct_tape(qnode)(state=np.array([1, -1, -1, 1]) / np.sqrt(4)) + res = tape.to_openqasm(precision=11) expected = dedent( """\ @@ -583,8 +555,8 @@ def qnode(state=None): return qml.expval(qml.PauliZ(0)) # construct the qnode circuit - qnode(state=np.array([1, 0, 1, 1])) - res = qnode.qtape.to_openqasm() + tape = qml.workflow.construct_tape(qnode)(state=np.array([1, 0, 1, 1])) + res = tape.to_openqasm() expected = dedent( """\ @@ -607,37 +579,27 @@ def qnode(state=None): def test_unsupported_gate(self): """Test an exception is raised if an unsupported operation is applied.""" - dev = qml.device("default.qubit", wires=4) - - @qml.qnode(dev) - def qnode(): - qml.S(wires=0) - qml.DoubleExcitationPlus(0.5, wires=[0, 1, 2, 3]) - return qml.expval(qml.PauliZ(0)) - - qnode() + tape = qml.tape.QuantumScript( + [qml.S(wires=0), qml.DoubleExcitationPlus(0.5, wires=[0, 1, 2, 3])], + [qml.expval(qml.PauliZ(0))], + ) with pytest.raises( ValueError, match="DoubleExcitationPlus not supported by the QASM serializer" ): - qnode.qtape.to_openqasm() + tape.to_openqasm() def test_rotations(self): """Test that observable rotations are correctly applied.""" - dev = qml.device("default.qubit", wires=3) - - @qml.qnode(dev) - def qnode(): - qml.Hadamard(wires=0) - qml.CNOT(wires=[0, 1]) - return [ + tape = qml.tape.QuantumScript( + [qml.Hadamard(wires=0), qml.CNOT(wires=[0, 1])], + [ qml.expval(qml.PauliX(0)), qml.expval(qml.PauliZ(1)), qml.expval(qml.Hadamard(2)), - ] - - qnode() - res = qnode.qtape.to_openqasm() + ], + ) + res = tape.to_openqasm() expected = dedent( """\ @@ -671,8 +633,8 @@ def qnode(): qml.expval(qml.Hadamard("b")), ] - qnode() - res = qnode.qtape.to_openqasm() + tape = qml.workflow.construct_tape(qnode)() + res = tape.to_openqasm() expected = dedent( """\ @@ -694,15 +656,8 @@ def qnode(): def test_precision(self): """Test that the QASM serializer takes into account the desired precision.""" - dev = qml.device("default.qubit", wires=["a"]) - - @qml.qnode(dev) - def qnode(): - qml.RX(np.pi, wires="a") - return qml.expval(qml.PauliZ("a")) - - qnode() - res = qnode.qtape.to_openqasm(precision=4) + tape = qml.tape.QuantumScript([qml.RX(np.pi, 0)], [qml.expval(qml.PauliZ(0))]) + res = tape.to_openqasm(precision=4) expected = dedent( """\ @@ -730,8 +685,8 @@ def qnode(param): qml.RX(param, wires="a") return qml.expval(qml.PauliZ("a")) - qnode(tf.Variable(1.2)) - res = qnode.qtape.to_openqasm() + tape = qml.workflow.construct_tape(qnode)(tf.Variable(1.2)) + res = tape.to_openqasm() expected = dedent( """\ @@ -778,8 +733,8 @@ def qnode(x): qml.expval(qml.Hadamard(2)), ] - qnode([0.1, 0.2]) - res = qnode.qtape.to_openqasm() + tape = qml.workflow.construct_tape(qnode)([0.1, 0.2]) + res = tape.to_openqasm() # Note: Qiskit hardcodes in pi as a QASM constant. # Here, we replace it with its numerical value. @@ -799,9 +754,8 @@ def qnode(state=None): qml.BasisState(state, wires=[0, 1, 2, 3]) return qml.expval(qml.PauliZ(0)) - # construct the qnode circuit - qnode(state=np.array([1, 0, 1, 1])) - res = qnode.qtape.to_openqasm(wires=dev.wires) + tape = qml.workflow.construct_tape(qnode)(state=np.array([1, 0, 1, 1])) + res = tape.to_openqasm(wires=dev.wires) expected = dev._circuit.qasm() assert res == expected @@ -824,8 +778,8 @@ def qnode(x): ] params = [0.1, 0.2] - qnode(params) - qasm = qnode.qtape.to_openqasm() + tape = qml.workflow.construct_tape(qnode)(params) + qasm = tape.to_openqasm() qc = self.qiskit.QuantumCircuit.from_qasm_str(qasm) gates = [g for g, _, _ in qc.data] diff --git a/tests/devices/test_default_mixed_jax.py b/tests/devices/test_default_mixed_jax.py index 53e778a4452..33d393a2c36 100644 --- a/tests/devices/test_default_mixed_jax.py +++ b/tests/devices/test_default_mixed_jax.py @@ -14,7 +14,6 @@ """ Tests for the ``default.mixed`` device for the JAX interface """ -import warnings # pylint: disable=protected-access from functools import partial @@ -26,14 +25,6 @@ from pennylane import numpy as pnp from pennylane.devices.default_mixed import DefaultMixed - -@pytest.fixture(autouse=True) -def suppress_tape_property_deprecation_warning(): - warnings.filterwarnings( - "ignore", "The tape/qtape property is deprecated", category=qml.PennyLaneDeprecationWarning - ) - - pytestmark = pytest.mark.jax jax = pytest.importorskip("jax") @@ -141,15 +132,15 @@ def circuit(a, b, c): grad_fn = jax.jit(jax.grad(circuit, argnums=[0, 1, 2])) res1 = grad_fn(a, b, c) - # the tape has reported both arguments as trainable - assert circuit.qtape.trainable_params == [0, 1, 2, 3] + assert len(res1) == 3 assert all(isinstance(r_, jax.Array) for r_ in res1) # make the second QNode argument a constant grad_fn = jax.grad(circuit, argnums=[0, 1]) res2 = grad_fn(a, b, c) - assert circuit.qtape.trainable_params == [0, 1] + assert len(res2) == 2 + assert all(isinstance(r_, jax.Array) for r_ in res2) assert qml.math.allclose(res1[:2], res2) @pytest.mark.parametrize("gradient_func", [qml.gradients.param_shift, None]) diff --git a/tests/gradients/core/test_adjoint_metric_tensor.py b/tests/gradients/core/test_adjoint_metric_tensor.py index 38a09d00ee0..e20097a8211 100644 --- a/tests/gradients/core/test_adjoint_metric_tensor.py +++ b/tests/gradients/core/test_adjoint_metric_tensor.py @@ -14,7 +14,6 @@ """ Unit tests for the adjoint_metric_tensor function. """ -import warnings import numpy as onp @@ -24,14 +23,6 @@ import pennylane as qml from pennylane import numpy as np - -@pytest.fixture(autouse=True) -def suppress_tape_property_deprecation_warning(): - warnings.filterwarnings( - "ignore", "The tape/qtape property is deprecated", category=qml.PennyLaneDeprecationWarning - ) - - fixed_pars = [-0.2, 0.2, 0.5, 0.3, 0.7] @@ -288,9 +279,10 @@ def circuit(*params): mt = qml.adjoint_metric_tensor(circuit)(*params) assert qml.math.allclose(mt, expected) - mt = qml.adjoint_metric_tensor(circuit.qtape) - expected = qml.math.reshape(expected, qml.math.shape(mt)) - assert qml.math.allclose(mt, expected) + tape = qml.workflow.construct_tape(circuit)(*params) + met_tens = qml.adjoint_metric_tensor(tape) + expected = qml.math.reshape(expected, qml.math.shape(met_tens)) + assert qml.math.allclose(met_tens, expected) @pytest.mark.jax @pytest.mark.skip("JAX does not support forward pass execution of the metric tensor.") @@ -311,9 +303,10 @@ def circuit(*params): return qml.expval(qml.PauliZ(0)) circuit(*j_params) - mt = qml.adjoint_metric_tensor(circuit.qtape) - expected = qml.math.reshape(expected, qml.math.shape(mt)) - assert qml.math.allclose(mt, expected) + tape = qml.workflow.construct_tape(circuit)(*j_params) + met_tens = qml.adjoint_metric_tensor(tape) + expected = qml.math.reshape(expected, qml.math.shape(met_tens)) + assert qml.math.allclose(met_tens, expected) mt = qml.adjoint_metric_tensor(circuit)(*j_params) assert qml.math.allclose(mt, expected) @@ -342,9 +335,10 @@ def circuit(*params): mt = qml.adjoint_metric_tensor(circuit)(*t_params) assert qml.math.allclose(mt, expected) - mt = qml.adjoint_metric_tensor(circuit.qtape) - expected = qml.math.reshape(expected, qml.math.shape(mt)) - assert qml.math.allclose(mt.detach().numpy(), expected) + tape = qml.workflow.construct_tape(circuit)(*t_params) + met_tens = qml.adjoint_metric_tensor(tape) + expected = qml.math.reshape(expected, qml.math.shape(met_tens)) + assert qml.math.allclose(met_tens.detach().numpy(), expected) interfaces = ["auto", "tf"] @@ -368,7 +362,8 @@ def circuit(*params): with tf.GradientTape(): circuit(*t_params) - mt = qml.adjoint_metric_tensor(circuit.qtape) + tape = qml.workflow.construct_tape(circuit)(*t_params) + mt = qml.adjoint_metric_tensor(tape) with tf.GradientTape(): mt = qml.adjoint_metric_tensor(circuit)(*t_params) diff --git a/tests/gradients/core/test_pulse_gradient.py b/tests/gradients/core/test_pulse_gradient.py index 1a52a02e66c..5fd9bf34937 100644 --- a/tests/gradients/core/test_pulse_gradient.py +++ b/tests/gradients/core/test_pulse_gradient.py @@ -16,7 +16,6 @@ """ import copy -import warnings import numpy as np import pytest @@ -31,13 +30,6 @@ ) -@pytest.fixture(autouse=True) -def suppress_tape_property_deprecation_warning(): - warnings.filterwarnings( - "ignore", "The tape/qtape property is deprecated", category=qml.PennyLaneDeprecationWarning - ) - - # pylint: disable=too-few-public-methods @pytest.mark.jax class TestSplitEvolOps: @@ -1181,17 +1173,17 @@ def qnode(params): qml.evolve(ham, atol=1e-6)(params, 0.1) return qml.expval(qml.PauliY(0) @ qml.PauliX(1)) - qnode.construct((params,), {}) + tape = qml.workflow.construct_tape(qnode)(params) num_split_times = 5 - qnode.tape.trainable_params = [0, 1, 2] + tape.trainable_params = [0, 1, 2] # FIXME: This test case is not updated to use the pytest-rng generated seed because I'm # unable to find a local salt that actually allows this test to pass. The 7123 here # is basically a magic number. Every other seed I tried fails. I believe this test # should be rewritten to use a better testing strategy because this currently goes # against the spirit of seeding. - tapes, fn = stoch_pulse_grad(qnode.tape, num_split_times=num_split_times, sampler_seed=7123) + tapes, fn = stoch_pulse_grad(tape, num_split_times=num_split_times, sampler_seed=7123) # Two generating terms with two shifts (X_0 and Z_0), one with eight shifts # (Y_0Y_1+0.4 X_1 has eigenvalues [-1.4, -0.6, 0.6, 1.4] yielding frequencies # [0.8, 1.2, 2.0, 2.8] and hence 2 * 4 = 8 shifts) @@ -1262,11 +1254,10 @@ def qnode(params_0, params_1): qml.evolve(ham_1)(params_1, 0.15) return qml.expval(qml.PauliY(0) @ qml.PauliZ(1)) - qnode.construct((params_0, params_1), {}) - + tape = qml.workflow.construct_tape(qnode)(params_0, params_1) num_split_times = 3 - qnode.tape.trainable_params = [0, 1, 2] - tapes, fn = stoch_pulse_grad(qnode.tape, num_split_times=num_split_times, sampler_seed=seed) + tape.trainable_params = [0, 1, 2] + tapes, fn = stoch_pulse_grad(tape, num_split_times=num_split_times, sampler_seed=seed) assert len(tapes) == 3 * 2 * num_split_times res = fn(qml.execute(tapes, dev, None)) diff --git a/tests/gradients/core/test_pulse_odegen.py b/tests/gradients/core/test_pulse_odegen.py index 128e6f76426..37e86692bb5 100644 --- a/tests/gradients/core/test_pulse_odegen.py +++ b/tests/gradients/core/test_pulse_odegen.py @@ -17,7 +17,6 @@ # pylint:disable=import-outside-toplevel, use-implicit-booleaness-not-comparison import copy -import warnings import numpy as np import pytest @@ -36,14 +35,6 @@ from pennylane.math import expand_matrix from pennylane.ops.qubit.special_unitary import pauli_basis_matrices, pauli_basis_strings - -@pytest.fixture(autouse=True) -def suppress_tape_property_deprecation_warning(): - warnings.filterwarnings( - "ignore", "The tape/qtape property is deprecated", category=qml.PennyLaneDeprecationWarning - ) - - X, Y, Z = qml.PauliX, qml.PauliY, qml.PauliZ @@ -1065,9 +1056,8 @@ def circuit(par): qml.evolve(H)(par, t=t) return qml.expval(Z(0)) - circuit.construct(([x, y],), {}) # TODO: remove once #2155 is resolved - tape_with_shots = circuit.tape.copy() + tape_with_shots = qml.workflow.construct_tape(circuit)([x, y]) tape_with_shots.trainable_params = [0, 1] tape_with_shots._shots = qml.measurements.Shots(shots) # pylint:disable=protected-access _tapes, fn = pulse_odegen(tape_with_shots, argnum=[0, 1]) @@ -1148,9 +1138,8 @@ def circuit(par): qml.evolve(H1)(par[1:], t=t) return qml.expval(Z(0)) - circuit.construct(([x, y, z],), {}) # TODO: remove once #2155 is resolved - tape_with_shots = circuit.tape.copy() + tape_with_shots = qml.workflow.construct_tape(circuit)([x, y, z]) tape_with_shots.trainable_params = [0, 1, 2] tape_with_shots._shots = qml.measurements.Shots(shots) # pylint:disable=protected-access _tapes, fn = pulse_odegen(tape_with_shots, argnum=[0, 1, 2]) diff --git a/tests/gradients/parameter_shift/test_parameter_shift_hessian.py b/tests/gradients/parameter_shift/test_parameter_shift_hessian.py index a0cd5117e11..bded0a8a5d9 100644 --- a/tests/gradients/parameter_shift/test_parameter_shift_hessian.py +++ b/tests/gradients/parameter_shift/test_parameter_shift_hessian.py @@ -13,7 +13,6 @@ # limitations under the License. """Tests for the gradients.param_shift_hessian module.""" -import warnings from itertools import product import pytest @@ -27,13 +26,6 @@ ) -@pytest.fixture(autouse=True) -def suppress_tape_property_deprecation_warning(): - warnings.filterwarnings( - "ignore", "The tape/qtape property is deprecated", category=qml.PennyLaneDeprecationWarning - ) - - class TestProcessArgnum: """Tests for the helper method _process_argnum.""" @@ -1507,12 +1499,11 @@ def circuit(params): return qml.probs([2, 3]) params = np.array([0.5, 0.5, 0.5], requires_grad=True) - circuit(params) result = qml.gradients.param_shift_hessian(circuit)(params) assert np.allclose(result, np.zeros((3, 3, 4)), atol=0, rtol=0) - tapes, _ = qml.gradients.param_shift_hessian(circuit.qtape) + tapes, _ = qml.gradients.param_shift_hessian(qml.workflow.construct_tape(circuit)(params)) assert tapes == [] @pytest.mark.xfail(reason="Update tracker for new return types") @@ -1602,9 +1593,8 @@ def circuit(x): x = np.array([0.6, -0.2], requires_grad=True) expected = qml.math.transpose(qml.jacobian(qml.jacobian(circuit))(x), (1, 2, 0)) - circuit(x) tapes, fn = qml.gradients.param_shift_hessian( - circuit.qtape, diagonal_shifts=diagonal_shifts + qml.workflow.construct_tape(circuit)(x), diagonal_shifts=diagonal_shifts ) # We expect the following tapes: @@ -1646,9 +1636,8 @@ def circuit(x): x = np.array([0.6, -0.2], requires_grad=True) expected = qml.math.transpose(qml.jacobian(qml.jacobian(circuit))(x), (1, 2, 0)) - circuit(x) tapes, fn = qml.gradients.param_shift_hessian( - circuit.qtape, off_diagonal_shifts=off_diagonal_shifts + qml.workflow.construct_tape(circuit)(x), off_diagonal_shifts=off_diagonal_shifts ) # We expect the following tapes: diff --git a/tests/measurements/test_classical_shadow.py b/tests/measurements/test_classical_shadow.py index 0340592c1da..f9ecf146e22 100644 --- a/tests/measurements/test_classical_shadow.py +++ b/tests/measurements/test_classical_shadow.py @@ -14,7 +14,6 @@ """Unit tests for the classical shadows measurement processes""" import copy -import warnings import autograd.numpy import pytest @@ -24,14 +23,6 @@ from pennylane.measurements import ClassicalShadowMP from pennylane.measurements.classical_shadow import ShadowExpvalMP - -@pytest.fixture(autouse=True) -def suppress_tape_property_deprecation_warning(): - warnings.filterwarnings( - "ignore", "The tape/qtape property is deprecated", category=qml.PennyLaneDeprecationWarning - ) - - # pylint: disable=dangerous-default-value, too-many-arguments @@ -262,9 +253,9 @@ def test_shape_matches(self, wires): shots = 100 circuit = get_circuit(wires, shots, True) - circuit.construct((), {}) + tape = qml.workflow.construct_tape(circuit)() - res = qml.execute([circuit.tape], circuit.device, None)[0] + res = qml.execute([tape], circuit.device, None)[0] expected_shape = qml.classical_shadow(wires=range(wires)).shape(shots, wires) assert res.shape == expected_shape @@ -458,9 +449,9 @@ def test_shape_matches(self): H = qml.PauliZ(0) circuit = hadamard_circuit(wires, shots) - circuit.construct((H,), {}) + tape = qml.workflow.construct_tape(circuit)(H) - res = qml.execute([circuit.tape], circuit.device, None)[0] + res = qml.execute([tape], circuit.device, None)[0] expected_shape = qml.shadow_expval(H).shape(shots, wires) assert res.shape == expected_shape @@ -800,9 +791,8 @@ def test_return_distribution(wires, interface, circuit_basis, basis_recipe): wires, basis=circuit_basis, shots=shots, interface=interface, device=device ) bits, recipes = circuit() # pylint: disable=unpacking-non-sequence - new_bits, new_recipes = circuit.tape.measurements[0].process( - circuit.tape, circuit.device.target_device - ) + tape = qml.workflow.construct_tape(circuit)() + new_bits, new_recipes = tape.measurements[0].process(tape, circuit.device.target_device) # test that the recipes follow a rough uniform distribution ratios = np.unique(recipes, return_counts=True)[1] / (wires * shots) @@ -849,8 +839,8 @@ def test_hadamard_expval(k=1, obs=obs_hadamard, expected=expected_hadamard): circuit = hadamard_circuit_legacy(3, shots=50000) actual = circuit(obs, k=k) - print(circuit.tape) - new_actual = circuit.tape.measurements[0].process(circuit.tape, circuit.device.target_device) + tape = qml.workflow.construct_tape(circuit)(obs, k=k) + new_actual = tape.measurements[0].process(tape, circuit.device.target_device) assert actual.shape == (len(obs_hadamard),) assert actual.dtype == np.float64 diff --git a/tests/measurements/test_state.py b/tests/measurements/test_state.py index 1e2b43e115b..6b9f2ce6dfe 100644 --- a/tests/measurements/test_state.py +++ b/tests/measurements/test_state.py @@ -12,7 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. """Unit tests for the state module""" -import warnings import numpy as np import pytest @@ -26,13 +25,6 @@ from pennylane.wires import WireError, Wires -@pytest.fixture(autouse=True) -def suppress_tape_property_deprecation_warning(): - warnings.filterwarnings( - "ignore", "The tape/qtape property is deprecated", category=qml.PennyLaneDeprecationWarning - ) - - class TestStateMP: """Tests for the State measurement process.""" @@ -247,8 +239,8 @@ def func(): qml.Hadamard(0) return state() - func() - obs = func.qtape.observables + tape = qml.workflow.construct_tape(func)() + obs = tape.observables assert len(obs) == 1 assert obs[0].return_type is State @@ -578,8 +570,8 @@ def func(): qml.Hadamard(0) return density_matrix(0) - func() - obs = func.qtape.observables + tape = qml.workflow.construct_tape(func)() + obs = tape.observables assert len(obs) == 1 assert obs[0].return_type is State diff --git a/tests/numpy/test_numpy_wrapper.py b/tests/numpy/test_numpy_wrapper.py index 8dbc68f320c..38f02c3ec14 100644 --- a/tests/numpy/test_numpy_wrapper.py +++ b/tests/numpy/test_numpy_wrapper.py @@ -16,7 +16,6 @@ modifies Autograd NumPy arrays so that they have an additional property, ``requires_grad``, that marks them as trainable/non-trainable. """ -import warnings import numpy as onp import pytest @@ -27,13 +26,6 @@ from pennylane.numpy.tensor import tensor_to_arraybox -@pytest.fixture(autouse=True) -def suppress_tape_property_deprecation_warning(): - warnings.filterwarnings( - "ignore", "The tape/qtape property is deprecated", category=qml.PennyLaneDeprecationWarning - ) - - @pytest.mark.unit class TestExtractTensors: """Tests for the extract_tensors function""" @@ -553,9 +545,9 @@ def circuit(phi=None): phi = np.tensor([[0.04439891, 0.14490549, 3.29725643, 2.51240058]]) - circuit(phi=phi) + tape = qml.workflow.construct_tape(circuit)(phi) - ops = circuit.tape.operations + ops = tape.operations assert len(ops) == 4 for op, p in zip(ops, phi[0]): # Test each rotation applied @@ -576,10 +568,10 @@ def circuit(phi=None): phi = np.tensor([[0.04439891, 0.14490549, 3.29725643]]) - circuit(phi=phi) + tape = qml.workflow.construct_tape(circuit)(phi) # Test the rotation applied - ops = circuit.tape.operations + ops = tape.operations assert len(ops) == 1 assert ops[0].name == "Rot" assert np.array_equal(ops[0].parameters, phi[0]) diff --git a/tests/ops/functions/test_map_wires.py b/tests/ops/functions/test_map_wires.py index 4b0816a187e..8f6abe2f02b 100644 --- a/tests/ops/functions/test_map_wires.py +++ b/tests/ops/functions/test_map_wires.py @@ -14,7 +14,6 @@ """ Unit tests for the qml.map_wires function """ -import warnings # pylint: disable=too-few-public-methods from functools import partial @@ -27,13 +26,6 @@ from pennylane.wires import Wires -@pytest.fixture(autouse=True) -def suppress_tape_property_deprecation_warning(): - warnings.filterwarnings( - "ignore", "The tape/qtape property is deprecated", category=qml.PennyLaneDeprecationWarning - ) - - def build_op(): """Return function to build nested operator.""" @@ -172,11 +164,11 @@ def qnode(): m_qnode = qml.map_wires(qnode, wire_map=wire_map) assert m_qnode() == qnode() - assert len(m_qnode.tape) == 2 - tapes, _ = m_qnode.transform_program((m_qnode.tape,)) + m_tape = qml.workflow.construct_tape(m_qnode)() + assert len(m_tape) == 2 - m_op = tapes[0].operations - m_obs = tapes[0].observables + m_op = m_tape.operations + m_obs = m_tape.observables qml.assert_equal(m_op[0], mapped_op) qml.assert_equal(m_obs[0], mapped_obs) @@ -197,10 +189,11 @@ def qfunc(): mapped_op_2 = qml.prod(qml.PauliX(4), qml.PauliY(3)) qnode = qml.QNode(qfunc, dev) m_qnode = qml.QNode(m_qfunc, dev) + m_tape = qml.workflow.construct_tape(m_qnode)() assert qml.math.allclose(m_qnode(), qnode()) - assert len(m_qnode.tape) == 4 - m_ops = m_qnode.tape.operations + assert len(m_tape) == 4 + m_ops = m_tape.operations assert isinstance(m_ops[0], Prod) assert isinstance(m_ops[1], Prod) qml.assert_equal(m_ops[0], mapped_op) diff --git a/tests/ops/functions/test_simplify.py b/tests/ops/functions/test_simplify.py index d555ba5439e..65b40d9d5d3 100644 --- a/tests/ops/functions/test_simplify.py +++ b/tests/ops/functions/test_simplify.py @@ -14,7 +14,6 @@ """ Unit tests for the qml.simplify function """ -import warnings # pylint: disable=too-few-public-methods import pytest @@ -24,13 +23,6 @@ from pennylane.tape import QuantumScript -@pytest.fixture(autouse=True) -def suppress_tape_property_deprecation_warning(): - warnings.filterwarnings( - "ignore", "The tape/qtape property is deprecated", category=qml.PennyLaneDeprecationWarning - ) - - def build_op(): """Return function to build nested operator.""" @@ -150,7 +142,8 @@ def qnode(): s_qnode = qml.simplify(qnode) assert s_qnode() == qnode() - [s_tape], _ = s_qnode.transform_program([s_qnode.tape]) + tape = qml.workflow.construct_tape(s_qnode)() + [s_tape], _ = s_qnode.transform_program([tape]) assert len(s_tape) == 2 s_op = s_tape.operations[0] @@ -184,8 +177,9 @@ def qfunc(): s_qnode = qml.QNode(s_qfunc, dev) assert (s_qnode() == qnode()).all() - assert len(s_qnode.tape) == 2 - s_op = s_qnode.tape.operations[0] + s_tape = qml.workflow.construct_tape(s_qnode)() + assert len(s_tape) == 2 + s_op = s_tape.operations[0] assert isinstance(s_op, qml.PauliZ) assert s_op.data == simplified_tape_op.data assert s_op.wires == simplified_tape_op.wires diff --git a/tests/ops/op_math/test_condition.py b/tests/ops/op_math/test_condition.py index 7476adec3cb..5255a1e7f91 100644 --- a/tests/ops/op_math/test_condition.py +++ b/tests/ops/op_math/test_condition.py @@ -23,7 +23,6 @@ files. """ -import warnings import numpy as np import pytest @@ -32,14 +31,6 @@ from pennylane.operation import Operator from pennylane.ops.op_math.condition import Conditional, ConditionalTransformError - -@pytest.fixture(autouse=True) -def suppress_tape_property_deprecation_warning(): - warnings.filterwarnings( - "ignore", "The tape/qtape property is deprecated", category=qml.PennyLaneDeprecationWarning - ) - - terminal_meas = [ qml.probs(wires=[1, 0]), qml.expval(qml.PauliZ(0)), @@ -653,7 +644,7 @@ def conditional(): circuit(0.5) def test_qnode(self): - """Test that qml.cond fallsback to Python when used + """Test that qml.cond falls back to Python when used within a QNode""" dev = qml.device("default.qubit", wires=1) @@ -664,18 +655,18 @@ def circuit(x): c(x, wires=0) return qml.probs(wires=0) - circuit(3) - ops = circuit.tape.operations + tape = qml.workflow.construct_tape(circuit)(3) + ops = tape.operations assert len(ops) == 1 assert ops[0].name == "RX" - circuit(2) - ops = circuit.tape.operations + tape = qml.workflow.construct_tape(circuit)(2) + ops = tape.operations assert len(ops) == 1 assert ops[0].name == "RY" assert np.allclose(ops[0].parameters[0], 2**2) - circuit(1) - ops = circuit.tape.operations + tape = qml.workflow.construct_tape(circuit)(1) + ops = tape.operations assert len(ops) == 1 assert ops[0].name == "RZ" diff --git a/tests/ops/op_math/test_exp.py b/tests/ops/op_math/test_exp.py index 4b9e52cde50..5abead84595 100644 --- a/tests/ops/op_math/test_exp.py +++ b/tests/ops/op_math/test_exp.py @@ -14,7 +14,6 @@ """Unit tests for the ``Exp`` class""" import copy import re -import warnings import pytest @@ -30,13 +29,6 @@ from pennylane.ops.op_math import Evolution, Exp -@pytest.fixture(autouse=True) -def suppress_tape_property_deprecation_warning(): - warnings.filterwarnings( - "ignore", "The tape/qtape property is deprecated", category=qml.PennyLaneDeprecationWarning - ) - - @pytest.mark.parametrize("constructor", (qml.exp, Exp)) class TestInitialization: """Test the initialization process and standard properties.""" @@ -1089,4 +1081,6 @@ def circuit(x, coeff): with pytest.warns(UserWarning): circuit(np.array(2.0), np.array(0.5)) - assert circuit.tape.trainable_params == [0, 1] + + tape = qml.workflow.construct_tape(circuit)(np.array(2.0), np.array(0.5)) + assert tape.trainable_params == [0, 1] diff --git a/tests/ops/op_math/test_linear_combination.py b/tests/ops/op_math/test_linear_combination.py index a252a68ce4f..d80923c3238 100644 --- a/tests/ops/op_math/test_linear_combination.py +++ b/tests/ops/op_math/test_linear_combination.py @@ -14,7 +14,6 @@ """ Tests for the LinearCombination class. """ -import warnings # pylint: disable=too-many-public-methods, too-few-public-methods from collections.abc import Iterable @@ -31,14 +30,6 @@ from pennylane.pauli import PauliSentence, PauliWord from pennylane.wires import Wires - -@pytest.fixture(autouse=True) -def suppress_tape_property_deprecation_warning(): - warnings.filterwarnings( - "ignore", "The tape/qtape property is deprecated", category=qml.PennyLaneDeprecationWarning - ) - - # Make test data in different interfaces, if installed COEFFS_PARAM_INTERFACE = [ ([-0.05, 0.17], 1.7, "autograd"), @@ -1593,8 +1584,8 @@ def circuit(): qml.RY(0.1, wires=0) return qml.expval(qml.simplify(qml.ops.LinearCombination([1.0, 2.0], [X(1), X(1)]))) - circuit() - pars = circuit.qtape.get_parameters(trainable_only=False) + tape = qml.workflow.construct_tape(circuit)() + pars = tape.get_parameters(trainable_only=False) # simplify worked and added 1. and 2. assert pars == [0.1, 3.0] diff --git a/tests/ops/op_math/test_prod.py b/tests/ops/op_math/test_prod.py index 432441d8be8..504f80cade3 100644 --- a/tests/ops/op_math/test_prod.py +++ b/tests/ops/op_math/test_prod.py @@ -14,7 +14,6 @@ """ Unit tests for the Prod arithmetic class of qubit operations """ -import warnings # pylint:disable=protected-access, unused-argument import gate_data as gd # a file containing matrix rep of each gate @@ -28,14 +27,6 @@ from pennylane.ops.op_math.prod import Prod, _swappable_ops, prod from pennylane.wires import Wires - -@pytest.fixture(autouse=True) -def suppress_tape_property_deprecation_warning(): - warnings.filterwarnings( - "ignore", "The tape/qtape property is deprecated", category=qml.PennyLaneDeprecationWarning - ) - - X, Y, Z = qml.PauliX, qml.PauliY, qml.PauliZ no_mat_ops = ( @@ -1537,8 +1528,8 @@ def circuit(x, U): x = qnp.array(0.1, requires_grad=False) U = qnp.array([[1.0, 0.0], [0.0, -1.0]], requires_grad=True) - circuit(x, U) - assert circuit.tape.trainable_params == [1] + tape = qml.workflow.construct_tape(circuit)(x, U) + assert tape.trainable_params == [1] class TestSortWires: diff --git a/tests/ops/op_math/test_sum.py b/tests/ops/op_math/test_sum.py index fd7905bcf62..f6c84ba2bff 100644 --- a/tests/ops/op_math/test_sum.py +++ b/tests/ops/op_math/test_sum.py @@ -16,7 +16,6 @@ """ # pylint: disable=eval-used, unused-argument -import warnings import gate_data as gd # a file containing matrix rep of each gate import numpy as np @@ -29,14 +28,6 @@ from pennylane.ops.op_math import Prod, Sum from pennylane.wires import Wires - -@pytest.fixture(autouse=True) -def suppress_tape_property_deprecation_warning(): - warnings.filterwarnings( - "ignore", "The tape/qtape property is deprecated", category=qml.PennyLaneDeprecationWarning - ) - - no_mat_ops = ( qml.Barrier, qml.WireCut, @@ -1312,8 +1303,8 @@ def circuit(): Sum(qml.s_prod(1.1, qml.PauliX(0)), qml.s_prod(qnp.array(2.2), qml.PauliY(1))) ) - circuit() - assert circuit.tape.trainable_params == [1] + tape = qml.workflow.construct_tape(circuit)() + assert tape.trainable_params == [1] # pylint: disable=too-few-public-methods diff --git a/tests/ops/test_meta.py b/tests/ops/test_meta.py index c451daff2dd..67cbf6f8a4d 100644 --- a/tests/ops/test_meta.py +++ b/tests/ops/test_meta.py @@ -12,7 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. """Unit tests for the Snapshot operation.""" -import warnings import numpy as np @@ -23,13 +22,6 @@ from pennylane import Snapshot -@pytest.fixture(autouse=True) -def suppress_tape_property_deprecation_warning(): - warnings.filterwarnings( - "ignore", "The tape/qtape property is deprecated", category=qml.PennyLaneDeprecationWarning - ) - - class TestBarrier: """Tests that the Barrier gate is correct.""" @@ -144,8 +136,8 @@ def circuit(): qml.ctrl(barrier, 2)() return qml.state() - circuit() - tape = circuit.tape.expand(stop_at=lambda op: op.name in ["Barrier", "PauliX", "CNOT"]) + tape = qml.workflow.construct_tape(circuit)() + tape = tape.expand(stop_at=lambda op: op.name in ["Barrier", "PauliX", "CNOT"]) assert tape.operations[1].name == "Barrier" assert tape.operations[4].name == "Barrier" diff --git a/tests/optimize/test_adaptive.py b/tests/optimize/test_adaptive.py index 06c6bb05780..fbc9bac1fb5 100644 --- a/tests/optimize/test_adaptive.py +++ b/tests/optimize/test_adaptive.py @@ -15,21 +15,12 @@ Unit tests for the ``AdaptiveOptimizer``. """ import copy -import warnings import pytest import pennylane as qml from pennylane import numpy as np - -@pytest.fixture(autouse=True) -def suppress_tape_property_deprecation_warning(): - warnings.filterwarnings( - "ignore", "The tape/qtape property is deprecated", category=qml.PennyLaneDeprecationWarning - ) - - symbols = ["H", "H", "H"] geometry = np.array( [[0.01076341, 0.04449877, 0.0], [0.98729513, 1.63059094, 0.0], [1.87262415, -0.00815842, 0.0]], @@ -109,8 +100,8 @@ def test_step_and_cost_drain(circuit, energy_ref, pool): for _ in range(4): circuit, energy, _ = opt.step_and_cost(circuit, copy.copy(pool), drain_pool=True) - _ = circuit() - selected_excitations = [op.wires for op in circuit.tape.operations[1:]] + tape = qml.workflow.construct_tape(circuit)() + selected_excitations = [op.wires for op in tape.operations[1:]] assert np.allclose(energy, energy_ref) # assert that the operator pool is drained, no repeated gates in the circuit @@ -130,8 +121,8 @@ def test_step_and_cost_nodrain(circuit, energy_ref, pool): for _ in range(4): circuit, energy, _ = opt.step_and_cost(circuit, pool, drain_pool=False) - circuit() - selected_excitations = [op.wires for op in circuit.tape.operations[1:]] + tape = qml.workflow.construct_tape(circuit)() + selected_excitations = [op.wires for op in tape.operations[1:]] assert np.allclose(energy, energy_ref, rtol=1e-4) # assert that the operator pool is not drained, there are repeated gates in the circuit @@ -144,12 +135,12 @@ def test_largest_gradient(circuit): opt = qml.AdaptiveOptimizer() circuit = opt.step(circuit, pool_exc) - circuit() - selected_gate = circuit.tape.operations[-1] + tape = qml.workflow.construct_tape(circuit)() + selected_gate = tape.operations[-1] # the reference gate is obtained manually assert selected_gate.name == "DoubleExcitation" - assert circuit.tape.operations[-1].wires == qml.wires.Wires([0, 1, 4, 5]) + assert tape.operations[-1].wires == qml.wires.Wires([0, 1, 4, 5]) @pytest.mark.parametrize( @@ -164,10 +155,10 @@ def test_append_gate(circuit): final_circuit = qml.optimize.adaptive.append_gate(circuit.func, param, [gate]) qnode = qml.QNode(final_circuit, dev) - _ = qnode() - qml.assert_equal(qnode.tape.operations[-1], gate) + tape = qml.workflow.construct_tape(qnode)() + qml.assert_equal(tape.operations[-1], gate) - final_circuit, fn = qml.optimize.adaptive.append_gate(qnode.tape, param, [gate]) + final_circuit, fn = qml.optimize.adaptive.append_gate(tape, param, [gate]) assert isinstance(final_circuit, list) assert isinstance(fn(final_circuit), qml.tape.QuantumScript) @@ -189,10 +180,11 @@ def test_qubit_rotation(circuit): opt = qml.AdaptiveOptimizer(param_steps=20) circuit = opt.step(circuit, pool, params_zero=False) expval = circuit() + tape = qml.workflow.construct_tape(circuit)() # rotation around X with np.pi gives expval(Z) = -1 assert np.allclose(expval, -1) - qml.assert_equal(circuit.tape.operations[-1], qml.RX(np.array([np.pi]), wires=0)) + qml.assert_equal(tape.operations[-1], qml.RX(np.array([np.pi]), wires=0)) @pytest.mark.parametrize( diff --git a/tests/qnn/test_keras.py b/tests/qnn/test_keras.py index 0959f8f397e..f0dd0aa79e4 100644 --- a/tests/qnn/test_keras.py +++ b/tests/qnn/test_keras.py @@ -14,7 +14,6 @@ """ Tests for the pennylane.qnn.keras module. """ -import warnings from collections import defaultdict import numpy as np @@ -22,14 +21,6 @@ import pennylane as qml - -@pytest.fixture(autouse=True) -def suppress_tape_property_deprecation_warning(): - warnings.filterwarnings( - "ignore", "The tape/qtape property is deprecated", category=qml.PennyLaneDeprecationWarning - ) - - KerasLayer = qml.qnn.keras.KerasLayer tf = pytest.importorskip("tensorflow", minversion="2") @@ -555,12 +546,12 @@ def test_construct(self, get_circuit, n_qubits, output_dim): x = tf.ones((1, n_qubits)) - layer.construct((x,), {}) + tape = qml.workflow.construct_tape(layer)(x) - assert layer.tape is not None + assert tape is not None assert ( - len(layer.tape.get_parameters(trainable_only=False)) - == len(layer.tape.get_parameters(trainable_only=True)) + 1 + len(tape.get_parameters(trainable_only=False)) + == len(tape.get_parameters(trainable_only=True)) + 1 ) diff --git a/tests/qnn/test_qnn_torch.py b/tests/qnn/test_qnn_torch.py index a5963a7ef4d..422f6cb1629 100644 --- a/tests/qnn/test_qnn_torch.py +++ b/tests/qnn/test_qnn_torch.py @@ -15,7 +15,6 @@ Tests for the pennylane.qnn.torch module. """ import math -import warnings from collections import defaultdict from unittest import mock @@ -25,14 +24,6 @@ import pennylane as qml from pennylane.qnn.torch import TorchLayer - -@pytest.fixture(autouse=True) -def suppress_tape_property_deprecation_warning(): - warnings.filterwarnings( - "ignore", "The tape/qtape property is deprecated", category=qml.PennyLaneDeprecationWarning - ) - - torch = pytest.importorskip("torch") # pylint: disable=unnecessary-dunder-call @@ -581,12 +572,12 @@ def test_construct(self, get_circuit, n_qubits): x = torch.ones(n_qubits) - layer.construct((x,), {}) + tape = qml.workflow.construct_tape(layer)(x) - assert layer.tape is not None + assert tape is not None assert ( - len(layer.tape.get_parameters(trainable_only=False)) - == len(layer.tape.get_parameters(trainable_only=True)) + 1 + len(tape.get_parameters(trainable_only=False)) + == len(tape.get_parameters(trainable_only=True)) + 1 ) diff --git a/tests/resource/test_error/test_error.py b/tests/resource/test_error/test_error.py index ef725e6ed27..82ff7c393d1 100644 --- a/tests/resource/test_error/test_error.py +++ b/tests/resource/test_error/test_error.py @@ -14,7 +14,6 @@ """ Test base AlgorithmicError class and its associated methods. """ -import warnings import numpy as np @@ -31,13 +30,6 @@ ) -@pytest.fixture(autouse=True) -def suppress_tape_property_deprecation_warning(): - warnings.filterwarnings( - "ignore", "The tape/qtape property is deprecated", category=qml.PennyLaneDeprecationWarning - ) - - class SimpleError(AlgorithmicError): def combine(self, other): return self.__class__(self.error + other.error) @@ -274,8 +266,8 @@ def circuit(): def test_computation(self): """Test that _compute_algo_error are adding up errors as expected.""" - _ = self.circuit() - algo_errors = _compute_algo_error(self.circuit.qtape) + tape = qml.workflow.construct_tape(self.circuit)() + algo_errors = _compute_algo_error(tape) assert len(algo_errors) == 3 assert all(error in algo_errors for error in self.errors_types) assert algo_errors["MultiplicativeError"].error == 0.31 * 0.24 diff --git a/tests/shadow/test_shadow_transforms.py b/tests/shadow/test_shadow_transforms.py index 9630cc6a9e5..7552786aca3 100644 --- a/tests/shadow/test_shadow_transforms.py +++ b/tests/shadow/test_shadow_transforms.py @@ -12,7 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. """Unit tests for the classical shadows transforms""" -import warnings # pylint: disable=too-few-public-methods import pytest @@ -22,13 +21,6 @@ from pennylane.shadows.transforms import _replace_obs -@pytest.fixture(autouse=True) -def suppress_tape_property_deprecation_warning(): - warnings.filterwarnings( - "ignore", "The tape/qtape property is deprecated", category=qml.PennyLaneDeprecationWarning - ) - - def hadamard_circuit(wires, shots=10000, interface="autograd"): """Hadamard circuit to put all qubits in equal superposition (locally)""" dev = qml.device("default.qubit", wires=wires, shots=shots) @@ -181,11 +173,11 @@ def test_large_state_warning(self): """Test that a warning is raised when the system to get the state of is large""" circuit = hadamard_circuit(8, shots=1) - circuit.construct([], {}) + tape = qml.workflow.construct_tape(circuit)() msg = "Differentiable state reconstruction for more than 8 qubits is not recommended" with pytest.warns(UserWarning, match=msg): - qml.shadows.shadow_state(circuit.qtape, wires=[0, 1, 2, 3, 4, 5, 6, 7], diffable=True) + qml.shadows.shadow_state(tape, wires=[0, 1, 2, 3, 4, 5, 6, 7], diffable=True) def test_multi_measurement_error(self): """Test that an error is raised when classical shadows is returned diff --git a/tests/tape/test_tape.py b/tests/tape/test_tape.py index 5ddbeb03d14..38546573ca4 100644 --- a/tests/tape/test_tape.py +++ b/tests/tape/test_tape.py @@ -14,7 +14,6 @@ """Unit tests for the QuantumTape""" # pylint: disable=protected-access,too-few-public-methods import copy -import warnings from collections import defaultdict import numpy as np @@ -35,13 +34,6 @@ from pennylane.tape import QuantumScript, QuantumTape, expand_tape_state_prep -@pytest.fixture(autouse=True) -def suppress_tape_property_deprecation_warning(): - warnings.filterwarnings( - "ignore", "The tape/qtape property is deprecated", category=qml.PennyLaneDeprecationWarning - ) - - def TestOperationMonkeypatching(): """Test that operations are monkeypatched only within the quantum tape""" with QuantumTape() as tape: @@ -2025,7 +2017,9 @@ def circuit(a, b): assert np.issubdtype(result[0].dtype, float) else: assert np.issubdtype(result.dtype, float) - assert circuit.qtape.numeric_type is float + + tape = qml.workflow.construct_tape(circuit)(0.3, 0.2) + assert tape.numeric_type is float @pytest.mark.parametrize( "ret", [qml.state(), qml.density_matrix(wires=[0, 1]), qml.density_matrix(wires=[2, 0])] @@ -2045,7 +2039,9 @@ def circuit(a, b): # Double-check the domain of the QNode output assert np.issubdtype(result.dtype, complex) - assert circuit.qtape.numeric_type is complex + + tape = qml.workflow.construct_tape(circuit)(0.3, 0.2) + assert tape.numeric_type is complex def test_sample_int(self): """Test that the tape can correctly determine the output domain for a @@ -2061,7 +2057,9 @@ def circuit(): # Double-check the domain of the QNode output assert np.issubdtype(result.dtype, int) - assert circuit.qtape.numeric_type is int + + tape = qml.workflow.construct_tape(circuit)() + assert tape.numeric_type is int # TODO: add cases for each interface once qml.Hermitian supports other # interfaces @@ -2088,7 +2086,9 @@ def circuit(a, b): # Double-check the domain of the QNode output assert np.issubdtype(result[0].dtype, float) - assert circuit.qtape.numeric_type is float + + tape = qml.workflow.construct_tape(circuit)(0.3, 0.2) + assert tape.numeric_type is float @pytest.mark.autograd def test_sample_real_and_int_eigvals(self): @@ -2117,7 +2117,8 @@ def circuit(a, b): assert result[0].dtype == float assert result[1].dtype == int - assert circuit.qtape.numeric_type == (float, int) + tape = qml.workflow.construct_tape(circuit)(0, 3) + assert tape.numeric_type == (float, int) def test_multi_type_measurements_numeric_type_error(self): """Test that querying the numeric type of a tape with several types of diff --git a/tests/test_compiler.py b/tests/test_compiler.py index 98126cbda50..85a04761029 100644 --- a/tests/test_compiler.py +++ b/tests/test_compiler.py @@ -14,7 +14,6 @@ """ Unit tests for the compiler subpackage. """ -import warnings # pylint: disable=import-outside-toplevel from unittest.mock import patch @@ -28,14 +27,6 @@ from pennylane.compiler.compiler import CompileError from pennylane.transforms.dynamic_one_shot import fill_in_value - -@pytest.fixture(autouse=True) -def suppress_tape_property_deprecation_warning(): - warnings.filterwarnings( - "ignore", "The tape/qtape property is deprecated", category=qml.PennyLaneDeprecationWarning - ) - - catalyst = pytest.importorskip("catalyst") jax = pytest.importorskip("jax") @@ -382,7 +373,7 @@ def inner(j): assert f(4, 7) == 28 # 4 * 7 def test_fallback_while_loop_qnode(self): - """Test that qml.while_loop inside a qnode fallsback to + """Test that qml.while_loop inside a qnode falls back to Python without qjit""" dev = qml.device("lightning.qubit", wires=1) @@ -398,9 +389,9 @@ def loop(v): assert jnp.allclose(circuit(1), -1.0) - res = circuit.tape.operations + tape = qml.workflow.construct_tape(circuit)(1) expected = [qml.PauliX(0) for i in range(4)] - _ = [qml.assert_equal(i, j) for i, j in zip(res, expected)] + _ = [qml.assert_equal(i, j) for i, j in zip(tape.operations, expected)] def test_dynamic_wires_for_loops(self): """Test for loops with iteration index-dependant wires.""" diff --git a/tests/test_observable.py b/tests/test_observable.py index b373347613e..80318eba80c 100644 --- a/tests/test_observable.py +++ b/tests/test_observable.py @@ -16,20 +16,10 @@ """ # pylint: disable=protected-access,cell-var-from-loop -import warnings - -import pytest import pennylane as qml -@pytest.fixture(autouse=True) -def suppress_tape_property_deprecation_warning(): - warnings.filterwarnings( - "ignore", "The tape/qtape property is deprecated", category=qml.PennyLaneDeprecationWarning - ) - - def test_pass_positional_wires_to_observable(): """Tests whether the ability to pass wires as positional argument is retained""" dev = qml.device("default.qubit", wires=1) @@ -40,5 +30,5 @@ def test_pass_positional_wires_to_observable(): def circuit(): return qml.expval(obs) - circuit() - assert obs in circuit.qtape.observables + tape = qml.workflow.construct_tape(circuit)() + assert obs in tape.observables diff --git a/tests/test_queuing.py b/tests/test_queuing.py index b1c394c59f6..1742286d35a 100644 --- a/tests/test_queuing.py +++ b/tests/test_queuing.py @@ -14,7 +14,6 @@ """ Unit tests for the :mod:`pennylane` :class:`QueuingManager` class. """ -import warnings from multiprocessing.dummy import Pool as ThreadPool import numpy as np @@ -24,13 +23,6 @@ from pennylane.queuing import AnnotatedQueue, QueuingError, QueuingManager, WrappedObj -@pytest.fixture(autouse=True) -def suppress_tape_property_deprecation_warning(): - warnings.filterwarnings( - "ignore", "The tape/qtape property is deprecated", category=qml.PennyLaneDeprecationWarning - ) - - # pylint: disable=use-implicit-booleaness-not-comparison, unnecessary-dunder-call class TestStopRecording: """Test the stop_recording method of QueuingManager.""" @@ -51,8 +43,7 @@ def my_circuit(): res.extend(my_op()) return qml.expval(qml.PauliZ(0)) - my_circuit.construct([], {}) - tape = my_circuit.qtape + tape = qml.workflow.construct_tape(my_circuit)() assert len(tape.operations) == 0 assert len(res) == 3 @@ -69,8 +60,7 @@ def my_circuit(): res.extend([op1, op2]) return qml.expval(qml.PauliZ(0)) - my_circuit.construct([], {}) - tape = my_circuit.qtape + tape = qml.workflow.construct_tape(my_circuit)() assert len(tape.operations) == 1 assert tape.operations[0] == res[1] @@ -106,8 +96,7 @@ def my_circuit(): my_op() return qml.state() - my_circuit.construct([], {}) - tape = my_circuit.qtape + tape = qml.workflow.construct_tape(my_circuit)() assert len(tape.operations) == 1 assert tape.operations[0].name == "Hadamard" @@ -125,7 +114,7 @@ def my_circuit(): result = my_circuit() assert len(result) == 0 - tape = my_circuit.qtape + tape = qml.workflow.construct_tape(my_circuit)() assert len(tape.operations) == 0 assert len(tape.measurements) == 0 diff --git a/tests/test_tracker.py b/tests/test_tracker.py index 4515404d840..cc455ba17c8 100644 --- a/tests/test_tracker.py +++ b/tests/test_tracker.py @@ -14,7 +14,6 @@ """ Unit tests for the Tracker and constructor """ -import warnings import pytest @@ -22,13 +21,6 @@ from pennylane import Tracker -@pytest.fixture(autouse=True) -def suppress_tape_property_deprecation_warning(): - warnings.filterwarnings( - "ignore", "The tape/qtape property is deprecated", category=qml.PennyLaneDeprecationWarning - ) - - class TestTrackerCoreBehavior: """Unittests for the tracker class""" @@ -265,11 +257,10 @@ def callback(totals=None, history=None, latest=None): wrapper = callback_wrapper() spy = mocker.spy(wrapper, "callback") - # initial execution to get qtape - circuit() + tape = qml.workflow.construct_tape(circuit)() with Tracker(circuit.device, callback=wrapper.callback) as tracker: - circuit.device.batch_execute([circuit.qtape, circuit.qtape]) + circuit.device.batch_execute([tape, tape]) assert tracker.totals == {"executions": 2, "batches": 1, "batch_len": 2} assert tracker.history == { diff --git a/tests/transforms/test_cliffordt_transform.py b/tests/transforms/test_cliffordt_transform.py index ac5e01d9072..759f44684f2 100644 --- a/tests/transforms/test_cliffordt_transform.py +++ b/tests/transforms/test_cliffordt_transform.py @@ -14,7 +14,6 @@ """Unit tests for the Clifford+T transform.""" import math -import warnings from functools import reduce import pytest @@ -31,14 +30,6 @@ ) from pennylane.transforms.optimization.optimization_utils import _fuse_global_phases - -@pytest.fixture(autouse=True) -def suppress_tape_property_deprecation_warning(): - warnings.filterwarnings( - "ignore", "The tape/qtape property is deprecated", category=qml.PennyLaneDeprecationWarning - ) - - _SKIP_GATES = (qml.Barrier, qml.Snapshot, qml.WireCut) _CLIFFORD_PHASE_GATES = _CLIFFORD_T_GATES + _SKIP_GATES @@ -172,10 +163,12 @@ def qfunc(): res1, res2 = original_qnode(), transfmd_qnode() assert qml.math.isclose(res1, res2, atol=1e-2) + tape = qml.workflow.construct_tape(transfmd_qnode)() + assert all( isinstance(op, _CLIFFORD_PHASE_GATES) or isinstance(getattr(op, "base", None), _CLIFFORD_PHASE_GATES) - for op in transfmd_qnode.tape.operations + for op in tape.operations ) @pytest.mark.parametrize("epsilon", [2e-2, 5e-2, 9e-2]) diff --git a/tests/transforms/test_compile.py b/tests/transforms/test_compile.py index 1f4ae3e5817..9f9dd3d4cf5 100644 --- a/tests/transforms/test_compile.py +++ b/tests/transforms/test_compile.py @@ -14,7 +14,6 @@ """ Unit tests for the ``compile`` transform. """ -import warnings from functools import partial import pytest @@ -34,13 +33,6 @@ from pennylane.wires import Wires -@pytest.fixture(autouse=True) -def suppress_tape_property_deprecation_warning(): - warnings.filterwarnings( - "ignore", "The tape/qtape property is deprecated", category=qml.PennyLaneDeprecationWarning - ) - - def build_qfunc(wires): def qfunc(x, y, z): qml.Hadamard(wires=wires[0]) @@ -94,7 +86,6 @@ def test_compile_mixed_tape_qfunc_transform(self): transformed_qfunc = compile(qfunc, pipeline) transformed_qnode = qml.QNode(transformed_qfunc, dev_3wires) - transformed_qnode(0.3, 0.4, 0.5) names_expected = ["Hadamard", "CNOT", "RX", "CY", "PauliY"] wires_expected = [ @@ -105,7 +96,8 @@ def test_compile_mixed_tape_qfunc_transform(self): Wires(wires[2]), ] - compare_operation_lists(transformed_qnode.qtape.operations, names_expected, wires_expected) + tape = qml.workflow.construct_tape(transformed_qnode)(0.3, 0.4, 0.5) + compare_operation_lists(tape.operations, names_expected, wires_expected) def test_compile_non_commuting_observables(self): """Test that compile works with non-commuting observables.""" @@ -149,10 +141,12 @@ def test_compile_empty_pipeline(self, wires): transformed_result = transformed_qnode(0.3, 0.4, 0.5) assert np.allclose(original_result, transformed_result) - names_expected = [op.name for op in qnode.qtape.operations] - wires_expected = [op.wires for op in qnode.qtape.operations] + tape = qml.workflow.construct_tape(qnode)(0.3, 0.4, 0.5) + names_expected = [op.name for op in tape.operations] + wires_expected = [op.wires for op in tape.operations] - compare_operation_lists(transformed_qnode.qtape.operations, names_expected, wires_expected) + transformed_tape = qml.workflow.construct_tape(transformed_qnode)(0.3, 0.4, 0.5) + compare_operation_lists(transformed_tape.operations, names_expected, wires_expected) @pytest.mark.parametrize(("wires"), [["a", "b", "c"], [0, 1, 2], [3, 1, 2], [0, "a", 4]]) def test_compile_default_pipeline(self, wires): @@ -179,7 +173,8 @@ def test_compile_default_pipeline(self, wires): Wires(wires[2]), ] - compare_operation_lists(transformed_qnode.qtape.operations, names_expected, wires_expected) + tape = qml.workflow.construct_tape(transformed_qnode)(0.3, 0.4, 0.5) + compare_operation_lists(tape.operations, names_expected, wires_expected) @pytest.mark.parametrize(("wires"), [["a", "b", "c"], [0, 1, 2], [3, 1, 2], [0, "a", 4]]) def test_compile_default_pipeline_qnode(self, wires): @@ -237,7 +232,8 @@ def test_compile_pipeline_with_non_default_arguments(self, wires): Wires([wires[1], wires[2]]), ] - compare_operation_lists(transformed_qnode.qtape.operations, names_expected, wires_expected) + tape = qml.workflow.construct_tape(transformed_qnode)(0.3, 0.4, 0.5) + compare_operation_lists(tape.operations, names_expected, wires_expected) def test_compile_decomposes_state_prep(self): """Test that compile decomposes state prep operations""" @@ -287,7 +283,8 @@ def test_compile_multiple_passes(self, wires): Wires([wires[1], wires[2]]), ] - compare_operation_lists(transformed_qnode.qtape.operations, names_expected, wires_expected) + tape = qml.workflow.construct_tape(transformed_qnode)(0.3, 0.4, 0.5) + compare_operation_lists(tape.operations, names_expected, wires_expected) @pytest.mark.parametrize(("wires"), [["a", "b", "c"], [0, 1, 2], [3, 1, 2], [0, "a", 4]]) def test_compile_decompose_into_basis_gates(self, wires): @@ -343,7 +340,8 @@ def test_compile_decompose_into_basis_gates(self, wires): Wires([]), ] - tansformed_ops = _fuse_global_phases(transformed_qnode.qtape.operations) + tape = qml.workflow.construct_tape(transformed_qnode)(0.3, 0.4, 0.5) + tansformed_ops = _fuse_global_phases(tape.operations) compare_operation_lists(tansformed_ops, names_expected, wires_expected) def test_compile_template(self): @@ -383,7 +381,8 @@ def qfunc(x, params): Wires([2, 0]), ] * 4 - compare_operation_lists(transformed_qnode.qtape.operations, names_expected, wires_expected) + tape = qml.workflow.construct_tape(transformed_qnode)(x, params) + compare_operation_lists(tape.operations, names_expected, wires_expected) def qfunc_emb(x, params): @@ -439,7 +438,8 @@ def test_compile_autograd(self, diff_method): ) # Check operation list - ops = transformed_qnode.qtape.operations + tape = qml.workflow.construct_tape(transformed_qnode)(x, params) + ops = tape.operations compare_operation_lists(ops, expected_op_list, expected_wires_list) @pytest.mark.torch @@ -471,7 +471,8 @@ def test_compile_torch(self): assert qml.math.allclose(original_params.grad, transformed_params.grad) # Check operation list - ops = transformed_qnode.qtape.operations + tape = qml.workflow.construct_tape(transformed_qnode)(transformed_x, transformed_params) + ops = tape.operations compare_operation_lists(ops, expected_op_list, expected_wires_list) @pytest.mark.tf @@ -507,7 +508,8 @@ def test_compile_tf(self, diff_method): assert qml.math.allclose(original_grad, transformed_grad) # Check operation list - ops = transformed_qnode.qtape.operations + tape = qml.workflow.construct_tape(transformed_qnode)(transformed_x, transformed_params) + ops = tape.operations compare_operation_lists(ops, expected_op_list, expected_wires_list) @pytest.mark.jax @@ -534,7 +536,8 @@ def test_compile_jax(self, diff_method): ) # Check operation list - ops = transformed_qnode.qtape.operations + tape = qml.workflow.construct_tape(transformed_qnode)(x, params) + ops = tape.operations compare_operation_lists(ops, expected_op_list, expected_wires_list) @pytest.mark.jax diff --git a/tests/transforms/test_defer_measurements.py b/tests/transforms/test_defer_measurements.py index 49c9c340d7b..31bbb88d9f5 100644 --- a/tests/transforms/test_defer_measurements.py +++ b/tests/transforms/test_defer_measurements.py @@ -15,7 +15,6 @@ Tests for the transform implementing the deferred measurement principle. """ import math -import warnings # pylint: disable=too-few-public-methods, too-many-arguments from functools import partial @@ -29,13 +28,6 @@ from pennylane.ops import Controlled -@pytest.fixture(autouse=True) -def suppress_tape_property_deprecation_warning(): - warnings.filterwarnings( - "ignore", "The tape/qtape property is deprecated", category=qml.PennyLaneDeprecationWarning - ) - - def test_broadcasted_postselection(mocker): """Test that broadcast_expand is used iff broadcasting with postselection.""" spy = mocker.spy(qml.transforms, "broadcast_expand") @@ -220,11 +212,13 @@ def qnode2(): assert isinstance(res1, type(res2)) assert res1.shape == res2.shape - assert len(qnode2.qtape.operations) == 0 - assert len(qnode1.qtape.measurements) == len(qnode2.qtape.measurements) + tape1 = qml.workflow.construct_tape(qnode1)() + tape2 = qml.workflow.construct_tape(qnode2)() + assert len(tape2.operations) == 0 + assert len(tape1.measurements) == len(tape2.measurements) # Check the measurements - for op1, op2 in zip(qnode1.qtape.measurements, qnode2.qtape.measurements): + for op1, op2 in zip(tape1.measurements, tape2.measurements): assert isinstance(op1, type(op2)) def test_reuse_wire_after_measurement(self): @@ -272,7 +266,8 @@ def qnode2(phi): assert np.isclose(qnode1(np.pi / 4), qnode2(np.pi / 4)) assert spy.call_count == 2 # once per device preprocessing - deferred_tapes, _ = qml.defer_measurements(qnode1.qtape) + tape1 = qml.workflow.construct_tape(qnode1)(np.pi / 4) + deferred_tapes, _ = qml.defer_measurements(tape1) deferred_tape = deferred_tapes[0] assert isinstance(deferred_tape.operations[5], Controlled) qml.assert_equal(deferred_tape.operations[5].base, qml.PauliZ(2)) @@ -314,14 +309,16 @@ def qnode2(phi, theta): assert spy.call_count == 4 - deferred_tapes1, _ = qml.defer_measurements(qnode1.qtape) + tape1 = qml.workflow.construct_tape(qnode1)(np.pi / 4, 3 * np.pi / 4) + deferred_tapes1, _ = qml.defer_measurements(tape1) deferred_tape1 = deferred_tapes1[0] assert len(deferred_tape1.wires) == 4 assert len(deferred_tape1.operations) == 6 assert np.allclose(res1, res2) - deferred_tapes2, _ = qml.defer_measurements(qnode2.qtape) + tape2 = qml.workflow.construct_tape(qnode2)(np.pi / 4, 3 * np.pi / 4) + deferred_tapes2, _ = qml.defer_measurements(tape2) deferred_tape2 = deferred_tapes2[0] assert len(deferred_tape2.wires) == 3 assert len(deferred_tape2.operations) == 4 @@ -361,8 +358,9 @@ def circ1(phi): qml.probs(wires=1), ] - assert len(circ1.qtape) == len(expected_circuit) - for op, expected_op in zip(circ1.qtape, expected_circuit): + tape1 = qml.workflow.construct_tape(circ1)(phi, shots=shots) + assert len(tape1) == len(expected_circuit) + for op, expected_op in zip(tape1, expected_circuit): qml.assert_equal(op, expected_op) @pytest.mark.parametrize("reduce_postselected", [None, True, False]) @@ -407,8 +405,9 @@ def circ1(phi): qml.probs(wires=1), ] - assert len(circ1.qtape) == len(expected_circuit) - for op, expected_op in zip(circ1.qtape, expected_circuit): + tape1 = qml.workflow.construct_tape(circ1)(phi, shots=shots) + assert len(tape1) == len(expected_circuit) + for op, expected_op in zip(tape1, expected_circuit): qml.assert_equal(op, expected_op) @pytest.mark.parametrize("reduce_postselected", [None, True, False]) @@ -488,8 +487,9 @@ def circ2(): ] ) - assert len(circ1.qtape) == len(expected_circuit) - for op, expected_op in zip(circ1.qtape, expected_circuit): + tape1 = qml.workflow.construct_tape(circ1)(phi, theta, shots=shots) + assert len(tape1) == len(expected_circuit) + for op, expected_op in zip(tape1, expected_circuit): qml.assert_equal(op, expected_op) @pytest.mark.parametrize("shots", [None, 1000, [1000, 1000]]) @@ -541,10 +541,11 @@ def circ2(x): assert np.allclose(circ1(param, shots=shots), circ2(param, shots=shots), atol=atol, rtol=0) expected_ops = [qml.RX(param, 0), qml.CNOT([0, 1]), qml.PauliX(0)] - assert circ1.qtape.operations == expected_ops + tape1 = qml.workflow.construct_tape(circ1)(param, shots=shots) + assert tape1.operations == expected_ops - assert len(circ1.qtape.measurements) == 1 - mp = circ1.qtape.measurements[0] + assert len(tape1.measurements) == 1 + mp = tape1.measurements[0] assert isinstance(mp, qml.measurements.ProbabilityMP) assert mp.mv is not None assert mp.mv.wires == qml.wires.Wires([1]) @@ -610,16 +611,18 @@ def func2(): assert isinstance(res1, type(res2)) assert res1.shape == res2.shape - assert len(qnode2.qtape.operations) == len(qnode1.qtape.operations) - assert len(qnode1.qtape.measurements) == len(qnode2.qtape.measurements) + tape1 = qml.workflow.construct_tape(qnode1)() + tape2 = qml.workflow.construct_tape(qnode2)() + assert len(tape2.operations) == len(tape1.operations) + assert len(tape1.measurements) == len(tape2.measurements) # Check the operations - for op1, op2 in zip(qnode1.qtape.operations, qnode2.qtape.operations): + for op1, op2 in zip(tape1.operations, tape2.operations): assert isinstance(op1, type(op2)) assert op1.data == op2.data # Check the measurements - for op1, op2 in zip(qnode1.qtape.measurements, qnode2.qtape.measurements): + for op1, op2 in zip(tape1.measurements, tape2.measurements): assert isinstance(op1, type(op2)) @pytest.mark.parametrize("mid_measure_wire, tp_wires", [(0, [1, 2, 3]), (0, [3, 1, 2])]) @@ -1376,16 +1379,18 @@ def qnode2(): assert np.allclose(res1, res2) - assert len(qnode2.qtape.operations) == len(qnode1.qtape.operations) - assert len(qnode1.qtape.measurements) == len(qnode2.qtape.measurements) + tape1 = qml.workflow.construct_tape(qnode1)() + tape2 = qml.workflow.construct_tape(qnode2)() + assert len(tape2.operations) == len(tape1.operations) + assert len(tape1.measurements) == len(tape2.measurements) # Check the operations - for op1, op2 in zip(qnode1.qtape.operations, qnode2.qtape.operations): + for op1, op2 in zip(tape1.operations, tape2.operations): assert isinstance(op1, type(op2)) assert np.allclose(op1.data, op2.data) # Check the measurements - for op1, op2 in zip(qnode1.qtape.measurements, qnode2.qtape.measurements): + for op1, op2 in zip(tape1.measurements, tape2.measurements): assert isinstance(op1, type(op2)) @pytest.mark.parametrize("template", [qml.StronglyEntanglingLayers, qml.BasicEntanglerLayers]) @@ -1413,17 +1418,18 @@ def qnode2(parameters): weights = np.random.random(size=shape) assert np.allclose(qnode1(weights), qnode2(weights)) - - assert len(qnode2.qtape.operations) == len(qnode1.qtape.operations) - assert len(qnode1.qtape.measurements) == len(qnode2.qtape.measurements) + tape1 = qml.workflow.construct_tape(qnode1)(weights) + tape2 = qml.workflow.construct_tape(qnode2)(weights) + assert len(tape2.operations) == len(tape1.operations) + assert len(tape1.measurements) == len(tape2.measurements) # Check the operations - for op1, op2 in zip(qnode1.qtape.operations, qnode2.qtape.operations): + for op1, op2 in zip(tape1.operations, tape2.operations): assert isinstance(op1, type(op2)) assert np.allclose(op1.data, op2.data) # Check the measurements - for op1, op2 in zip(qnode1.qtape.measurements, qnode2.qtape.measurements): + for op1, op2 in zip(tape1.measurements, tape2.measurements): assert isinstance(op1, type(op2)) @@ -1446,8 +1452,6 @@ def circ(x, y): qml.measure(0) return qml.expval(qml.PauliZ(1)) - _ = circ(1.0, 2.0) - expected = [ qml.RX(1.0, 0), qml.CNOT([0, 2]), @@ -1456,7 +1460,8 @@ def circ(x, y): qml.RZ(3.0, 1), ] - assert circ.qtape.operations == expected + tape = qml.workflow.construct_tape(circ)(1.0, 2.0) + assert tape.operations == expected def test_correct_cnot_for_reset(self): """Test that a CNOT is applied from the wire that stores the measurement @@ -1487,8 +1492,9 @@ def qnode2(x): qml.expval(qml.PauliZ(1)), ] - assert len(qnode2.qtape.circuit) == len(expected_circuit) - for actual, expected in zip(qnode2.qtape.circuit, expected_circuit): + tape2 = qml.workflow.construct_tape(qnode2)(0.123) + assert len(tape2.circuit) == len(expected_circuit) + for actual, expected in zip(tape2.circuit, expected_circuit): qml.assert_equal(actual, expected) def test_measurements_add_new_qubits(self): @@ -1571,7 +1577,8 @@ def qnode(p, x, y): qml.expval(qml.PauliZ(2)), ] - deferred_tapes, _ = qml.defer_measurements(qnode.qtape) + tape = qml.workflow.construct_tape(qnode)(0.123, 0.456, 0.789) + deferred_tapes, _ = qml.defer_measurements(tape) deferred_tape = deferred_tapes[0] assert len(deferred_tape.circuit) == len(expected_circuit) for actual, expected in zip(deferred_tape.circuit, expected_circuit): diff --git a/tests/transforms/test_optimization/test_cancel_inverses.py b/tests/transforms/test_optimization/test_cancel_inverses.py index f62603aac2d..d571a2a1f43 100644 --- a/tests/transforms/test_optimization/test_cancel_inverses.py +++ b/tests/transforms/test_optimization/test_cancel_inverses.py @@ -14,7 +14,6 @@ """ Unit tests for the optimization transform ``cancel_inverses``. """ -import warnings import pytest from utils import compare_operation_lists @@ -25,13 +24,6 @@ from pennylane.wires import Wires -@pytest.fixture(autouse=True) -def suppress_tape_property_deprecation_warning(): - warnings.filterwarnings( - "ignore", "The tape/qtape property is deprecated", category=qml.PennyLaneDeprecationWarning - ) - - class TestCancelInverses: """Test that adjacent inverse gates are cancelled.""" @@ -250,7 +242,8 @@ def test_cancel_inverses_autograd(self): ) # Check operation list - ops = transformed_qnode.qtape.operations + tape = qml.workflow.construct_tape(transformed_qnode)(input) + ops = tape.operations compare_operation_lists(ops, expected_op_list, expected_wires_list) @pytest.mark.torch @@ -277,7 +270,8 @@ def test_cancel_inverses_torch(self): assert qml.math.allclose(original_input.grad, transformed_input.grad) # Check operation list - ops = transformed_qnode.qtape.operations + tape = qml.workflow.construct_tape(transformed_qnode)(transformed_input) + ops = tape.operations compare_operation_lists(ops, expected_op_list, expected_wires_list) @pytest.mark.tf @@ -309,7 +303,8 @@ def test_cancel_inverses_tf(self): assert qml.math.allclose(original_grad, transformed_grad) # Check operation list - ops = transformed_qnode.qtape.operations + tape = qml.workflow.construct_tape(transformed_qnode)(transformed_input) + ops = tape.operations compare_operation_lists(ops, expected_op_list, expected_wires_list) @pytest.mark.jax @@ -332,7 +327,8 @@ def test_cancel_inverses_jax(self): ) # Check operation list - ops = transformed_qnode.qtape.operations + tape = qml.workflow.construct_tape(transformed_qnode)(input) + ops = tape.operations compare_operation_lists(ops, expected_op_list, expected_wires_list) @@ -408,8 +404,8 @@ def new_circuit(a): cancel_inverses(qfunc_circuit)(a) return qml.expval(qml.PauliX(0) @ qml.PauliX(2)) - new_circuit([0.1, 0.2]) - assert len(new_circuit.tape.operations) == 5 + tape = qml.workflow.construct_tape(new_circuit)([0.1, 0.2]) + assert len(tape.operations) == 5 def test_qnode(self): """Test the transform on a qnode directly.""" diff --git a/tests/transforms/test_optimization/test_commute_controlled.py b/tests/transforms/test_optimization/test_commute_controlled.py index 4ebc346cbd7..ad3c60495a1 100644 --- a/tests/transforms/test_optimization/test_commute_controlled.py +++ b/tests/transforms/test_optimization/test_commute_controlled.py @@ -14,7 +14,6 @@ """ Unit tests for the optimization transform ``commute_controlled``. """ -import warnings import pytest from utils import check_matrix_equivalence, compare_operation_lists @@ -25,13 +24,6 @@ from pennylane.wires import Wires -@pytest.fixture(autouse=True) -def suppress_tape_property_deprecation_warning(): - warnings.filterwarnings( - "ignore", "The tape/qtape property is deprecated", category=qml.PennyLaneDeprecationWarning - ) - - class TestCommuteControlled: """Tests for single-qubit gates being pushed through controlled gates.""" @@ -363,7 +355,8 @@ def test_commute_controlled_autograd(self): ) # Check operation list - ops = transformed_qnode.qtape.operations + tape = qml.workflow.construct_tape(transformed_qnode)(input) + ops = tape.operations compare_operation_lists(ops, expected_op_list, expected_wires_list) @pytest.mark.torch @@ -390,7 +383,8 @@ def test_commute_controlled_torch(self): assert qml.math.allclose(original_input.grad, transformed_input.grad) # Check operation list - ops = transformed_qnode.qtape.operations + tape = qml.workflow.construct_tape(transformed_qnode)(transformed_input) + ops = tape.operations compare_operation_lists(ops, expected_op_list, expected_wires_list) @pytest.mark.tf @@ -422,7 +416,8 @@ def test_commute_controlled_tf(self): assert qml.math.allclose(original_grad, transformed_grad) # Check operation list - ops = transformed_qnode.qtape.operations + tape = qml.workflow.construct_tape(transformed_qnode)(transformed_input) + ops = tape.operations compare_operation_lists(ops, expected_op_list, expected_wires_list) @pytest.mark.jax @@ -445,7 +440,8 @@ def test_commute_controlled_jax(self): ) # Check operation list - ops = transformed_qnode.qtape.operations + tape = qml.workflow.construct_tape(transformed_qnode)(input) + ops = tape.operations compare_operation_lists(ops, expected_op_list, expected_wires_list) @@ -520,8 +516,8 @@ def new_circuit(): commute_controlled(qfunc_circuit)() return qml.expval(qml.PauliX(0) @ qml.PauliX(2)) - new_circuit() - assert len(new_circuit.tape.operations) == 7 + tape = qml.workflow.construct_tape(new_circuit)() + assert len(tape.operations) == 7 names_expected = ["CNOT", "Toffoli", "PauliX", "RX", "CRX", "SX", "PauliX"] wires_expected = [ @@ -533,7 +529,7 @@ def new_circuit(): Wires(1), Wires(1), ] - compare_operation_lists(new_circuit.tape.operations, names_expected, wires_expected) + compare_operation_lists(tape.operations, names_expected, wires_expected) def test_qnode(self): """Test the transform on a qnode directly.""" diff --git a/tests/transforms/test_optimization/test_merge_amplitude_embedding.py b/tests/transforms/test_optimization/test_merge_amplitude_embedding.py index c672a0d7a62..eb5b40fe3b8 100644 --- a/tests/transforms/test_optimization/test_merge_amplitude_embedding.py +++ b/tests/transforms/test_optimization/test_merge_amplitude_embedding.py @@ -14,7 +14,6 @@ """ Unit tests for the optimization transform ``merge_amplitude_embedding``. """ -import warnings import pytest @@ -23,13 +22,6 @@ from pennylane.transforms.optimization import merge_amplitude_embedding -@pytest.fixture(autouse=True) -def suppress_tape_property_deprecation_warning(): - warnings.filterwarnings( - "ignore", "The tape/qtape property is deprecated", category=qml.PennyLaneDeprecationWarning - ) - - class TestMergeAmplitudeEmbedding: """Test that amplitude embedding gates are combined into a single.""" @@ -110,7 +102,8 @@ def qnode(): return qml.state() res = qnode() - assert qnode.tape.batch_size == 2 + tape = qml.workflow.construct_tape(qnode)() + assert tape.batch_size == 2 # |001> and |100> expected = np.array([[0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0]]) diff --git a/tests/transforms/test_optimization/test_merge_rotations.py b/tests/transforms/test_optimization/test_merge_rotations.py index d00f5a9e9e9..4de97e4b93a 100644 --- a/tests/transforms/test_optimization/test_merge_rotations.py +++ b/tests/transforms/test_optimization/test_merge_rotations.py @@ -16,7 +16,6 @@ """ # pylint: disable=too-many-arguments -import warnings import pytest from utils import compare_operation_lists @@ -27,13 +26,6 @@ from pennylane.wires import Wires -@pytest.fixture(autouse=True) -def suppress_tape_property_deprecation_warning(): - warnings.filterwarnings( - "ignore", "The tape/qtape property is deprecated", category=qml.PennyLaneDeprecationWarning - ) - - class TestMergeRotations: """Test that adjacent rotation gates of the same type will add the angles.""" @@ -337,7 +329,8 @@ def test_merge_rotations_autograd(self): ) # Check operation list - ops = transformed_qnode.qtape.operations + tape = qml.workflow.construct_tape(transformed_qnode)(input) + ops = tape.operations compare_operation_lists(ops, expected_op_list, expected_wires_list) @pytest.mark.torch @@ -364,7 +357,8 @@ def test_merge_rotations_torch(self): assert qml.math.allclose(original_input.grad, transformed_input.grad) # Check operation list - ops = transformed_qnode.qtape.operations + tape = qml.workflow.construct_tape(transformed_qnode)(transformed_input) + ops = tape.operations compare_operation_lists(ops, expected_op_list, expected_wires_list) @pytest.mark.tf @@ -396,7 +390,8 @@ def test_merge_rotations_tf(self): assert qml.math.allclose(original_grad, transformed_grad) # Check operation list - ops = transformed_qnode.qtape.operations + tape = qml.workflow.construct_tape(transformed_qnode)(transformed_input) + ops = tape.operations compare_operation_lists(ops, expected_op_list, expected_wires_list) @pytest.mark.jax @@ -419,7 +414,8 @@ def test_merge_rotations_jax(self): ) # Check operation list - ops = transformed_qnode.qtape.operations + tape = qml.workflow.construct_tape(transformed_qnode)(input) + ops = tape.operations compare_operation_lists(ops, expected_op_list, expected_wires_list) @pytest.mark.jax @@ -516,8 +512,8 @@ def new_circuit(a): merge_rotations(qfunc_circuit)(a) return qml.expval(qml.PauliX(0) @ qml.PauliX(2)) - new_circuit([0.1, 0.2, 0.3, 0.4]) - ops = new_circuit.tape.operations + tape = qml.workflow.construct_tape(new_circuit)([0.1, 0.2, 0.3, 0.4]) + ops = tape.operations compare_operation_lists(ops, expected_op_list, expected_wires_list) def test_qnode(self): diff --git a/tests/transforms/test_optimization/test_pattern_matching.py b/tests/transforms/test_optimization/test_pattern_matching.py index 698ca044c83..e78008e0ca9 100644 --- a/tests/transforms/test_optimization/test_pattern_matching.py +++ b/tests/transforms/test_optimization/test_pattern_matching.py @@ -14,7 +14,6 @@ """ Unit tests for the optimization transform ``pattern_matching_optimization``. """ -import warnings # pylint: disable=too-many-statements import pytest @@ -31,13 +30,6 @@ ) -@pytest.fixture(autouse=True) -def suppress_tape_property_deprecation_warning(): - warnings.filterwarnings( - "ignore", "The tape/qtape property is deprecated", category=qml.PennyLaneDeprecationWarning - ) - - class TestPatternMatchingOptimization: """Pattern matching circuit optimization tests.""" @@ -75,10 +67,12 @@ def circuit(): cnots_qnode = qml.specs(qnode)()["resources"].gate_types["CNOT"] cnots_optimized_qnode = qml.specs(optimized_qnode)()["resources"].gate_types["CNOT"] - assert len(qnode.qtape.operations) == 8 + tape = qml.workflow.construct_tape(qnode)() + assert len(tape.operations) == 8 assert cnots_qnode == 4 - assert len(optimized_qnode.qtape.operations) == 7 + optimized_tape = qml.workflow.construct_tape(optimized_qnode)() + assert len(optimized_tape.operations) == 7 assert cnots_optimized_qnode == 3 assert qnode_res == optimized_qnode_res @@ -150,10 +144,12 @@ def circuit(): cnots_qnode = qml.specs(qnode)()["resources"].gate_types["CNOT"] cnots_optimized_qnode = qml.specs(optimized_qnode)()["resources"].gate_types["CNOT"] - assert len(qnode.qtape.operations) == 8 + tape = qml.workflow.construct_tape(qnode)() + assert len(tape.operations) == 8 assert cnots_qnode == 4 - assert len(optimized_qnode.qtape.operations) == 7 + optimized_tape = qml.workflow.construct_tape(optimized_qnode)() + assert len(optimized_tape.operations) == 7 assert cnots_optimized_qnode == 3 assert qnode_res == optimized_qnode_res @@ -190,10 +186,12 @@ def circuit(): cnots_qnode = qml.specs(qnode)()["resources"].gate_types["CNOT"] cnots_optimized_qnode = qml.specs(optimized_qnode)()["resources"].gate_types["CNOT"] - assert len(qnode.qtape.operations) == 8 + tape = qml.workflow.construct_tape(qnode)() + assert len(tape.operations) == 8 assert cnots_qnode == 4 - assert len(optimized_qnode.qtape.operations) == 8 + optimized_tape = qml.workflow.construct_tape(optimized_qnode)() + assert len(optimized_tape.operations) == 8 assert cnots_optimized_qnode == 4 assert qnode_res == optimized_qnode_res @@ -231,10 +229,12 @@ def circuit(): "Adjoint(S)" ] - assert len(qnode.qtape.operations) == 8 + tape = qml.workflow.construct_tape(qnode)() + assert len(tape.operations) == 8 assert s_qnode == 5 - assert len(optimized_qnode.qtape.operations) == 5 + optimized_tape = qml.workflow.construct_tape(optimized_qnode)() + assert len(optimized_tape.operations) == 5 assert s_adjoint_optimized_qnode == 1 assert qnode_res == optimized_qnode_res @@ -277,10 +277,12 @@ def circuit(): toffolis_qnode = qml.specs(qnode)()["resources"].gate_types["Toffoli"] toffolis_optimized_qnode = qml.specs(optimized_qnode)()["resources"].gate_types["Toffoli"] - assert len(qnode.qtape.operations) == 11 + tape = qml.workflow.construct_tape(qnode)() + assert len(tape.operations) == 11 assert toffolis_qnode == 2 - assert len(optimized_qnode.qtape.operations) == 10 + optimized_tape = qml.workflow.construct_tape(optimized_qnode)() + assert len(optimized_tape.operations) == 10 assert toffolis_optimized_qnode == 0 assert qnode_res == optimized_qnode_res @@ -327,11 +329,13 @@ def circuit(): swap_optimized_qnode = gate_qnode_optimized["SWAP"] cnot_optimized_qnode = gate_qnode_optimized["CNOT"] - assert len(qnode.qtape.operations) == 8 + tape = qml.workflow.construct_tape(qnode)() + assert len(tape.operations) == 8 assert swap_qnode == 1 assert cnot_qnode == 1 - assert len(optimized_qnode.qtape.operations) == 10 + optimized_tape = qml.workflow.construct_tape(optimized_qnode)() + assert len(optimized_tape.operations) == 10 assert swap_optimized_qnode == 0 assert cnot_optimized_qnode == 4 @@ -377,11 +381,13 @@ def circuit(): swap_optimized_qnode = gate_qnode_optimized["SWAP"] cnot_optimized_qnode = gate_qnode_optimized["CNOT"] - assert len(qnode.qtape.operations) == 11 + tape = qml.workflow.construct_tape(qnode)() + assert len(tape.operations) == 11 assert swap_qnode == 4 assert cnot_qnode == 1 - assert len(optimized_qnode.qtape.operations) == 7 + optimized_tape = qml.workflow.construct_tape(optimized_qnode)() + assert len(optimized_tape.operations) == 7 assert swap_optimized_qnode == 0 assert cnot_optimized_qnode == 1 @@ -427,11 +433,13 @@ def circuit(): cswap_optimized_qnode = gate_qnode_optimized["CSWAP"] cnot_optimized_qnode = gate_qnode_optimized["CNOT"] - assert len(qnode.qtape.operations) == 11 + tape = qml.workflow.construct_tape(qnode)() + assert len(tape.operations) == 11 assert cswap_qnode == 4 assert cnot_qnode == 1 - assert len(optimized_qnode.qtape.operations) == 7 + optimized_tape = qml.workflow.construct_tape(optimized_qnode)() + assert len(optimized_tape.operations) == 7 assert cswap_optimized_qnode == 0 assert cnot_optimized_qnode == 1 @@ -485,11 +493,13 @@ def circuit(x, y): rz_qnode = qml.specs(qnode)(0.1, 0.2)["resources"].gate_types["RZ"] rz_optimized_qnode = qml.specs(optimized_qnode)(0.1, 0.2)["resources"].gate_types["RZ"] - assert len(qnode.qtape.operations) == 14 + tape = qml.workflow.construct_tape(qnode)(0.1, 0.2) + assert len(tape.operations) == 14 assert rx_qnode == 2 assert rz_qnode == 4 - assert len(optimized_qnode.qtape.operations) == 8 + optimized_tape = qml.workflow.construct_tape(optimized_qnode)(0.1, 0.2) + assert len(optimized_tape.operations) == 8 assert rx_optimized_qnode == 0 assert rz_optimized_qnode == 0 @@ -538,10 +548,12 @@ def circuit(): cnots_qnode = qml.specs(qnode)()["resources"].gate_types["CNOT"] cnots_optimized_qnode = qml.specs(optimized_qnode)()["resources"].gate_types["CNOT"] - assert len(qnode.qtape.operations) == 7 + tape = qml.workflow.construct_tape(qnode)() + assert len(tape.operations) == 7 assert cnots_qnode == 3 - assert len(optimized_qnode.qtape.operations) == 1 + optimized_tape = qml.workflow.construct_tape(optimized_qnode)() + assert len(optimized_tape.operations) == 1 assert cnots_optimized_qnode == 1 assert qnode_res == optimized_qnode_res @@ -624,10 +636,12 @@ def mod_5_4(): cnots_qnode = qml.specs(qnode)()["resources"].gate_types["CNOT"] cnots_optimized_qnode = qml.specs(optimized_qnode)()["resources"].gate_types["CNOT"] - assert len(qnode.qtape.operations) == 51 + tape = qml.workflow.construct_tape(qnode)() + assert len(tape.operations) == 51 assert cnots_qnode == 28 - assert len(optimized_qnode.qtape.operations) == 49 + optimized_tape = qml.workflow.construct_tape(optimized_qnode)() + assert len(optimized_tape.operations) == 49 assert cnots_optimized_qnode == 26 assert qnode_res == optimized_qnode_res @@ -749,10 +763,12 @@ def vbe_adder_3(): cnots_qnode = qml.specs(qnode)()["resources"].gate_types["CNOT"] cnots_optimized_qnode = qml.specs(optimized_qnode)()["resources"].gate_types["CNOT"] - assert len(qnode.qtape.operations) == 89 + tape = qml.workflow.construct_tape(qnode)() + assert len(tape.operations) == 89 assert cnots_qnode == 50 - assert len(optimized_qnode.qtape.operations) == 84 + optimized_tape = qml.workflow.construct_tape(optimized_qnode)() + assert len(optimized_tape.operations) == 84 assert cnots_optimized_qnode == 45 assert qnode_res == optimized_qnode_res diff --git a/tests/transforms/test_optimization/test_single_qubit_fusion.py b/tests/transforms/test_optimization/test_single_qubit_fusion.py index a66752e571d..8cf38469c76 100644 --- a/tests/transforms/test_optimization/test_single_qubit_fusion.py +++ b/tests/transforms/test_optimization/test_single_qubit_fusion.py @@ -14,8 +14,6 @@ """ Unit tests for the optimization transform ``single_qubit_fusion``. """ -import warnings - import pytest from utils import check_matrix_equivalence, compare_operation_lists @@ -25,13 +23,6 @@ from pennylane.wires import Wires -@pytest.fixture(autouse=True) -def suppress_tape_property_deprecation_warning(): - warnings.filterwarnings( - "ignore", "The tape/qtape property is deprecated", category=qml.PennyLaneDeprecationWarning - ) - - class TestSingleQubitFusion: """Test that sequences of any single-qubit rotations are fully fused.""" @@ -233,8 +224,8 @@ def test_single_qubit_fusion_autograd(self): ) # Check operation list - ops = transformed_qnode.qtape.operations - compare_operation_lists(ops, expected_op_list, expected_wires_list) + tape = qml.workflow.construct_tape(transformed_qnode)(input) + compare_operation_lists(tape.operations, expected_op_list, expected_wires_list) @pytest.mark.torch def test_single_qubit_fusion_torch(self): @@ -260,8 +251,8 @@ def test_single_qubit_fusion_torch(self): assert qml.math.allclose(original_input.grad, transformed_input.grad) # Check operation list - ops = transformed_qnode.qtape.operations - compare_operation_lists(ops, expected_op_list, expected_wires_list) + tape = qml.workflow.construct_tape(transformed_qnode)(transformed_input) + compare_operation_lists(tape.operations, expected_op_list, expected_wires_list) @pytest.mark.tf def test_single_qubit_fusion_tf(self): @@ -292,8 +283,8 @@ def test_single_qubit_fusion_tf(self): assert qml.math.allclose(original_grad, transformed_grad) # Check operation list - ops = transformed_qnode.qtape.operations - compare_operation_lists(ops, expected_op_list, expected_wires_list) + tape = qml.workflow.construct_tape(transformed_qnode)(transformed_input) + compare_operation_lists(tape.operations, expected_op_list, expected_wires_list) @pytest.mark.jax def test_single_qubit_fusion_jax(self): @@ -315,8 +306,8 @@ def test_single_qubit_fusion_jax(self): ) # Check operation list - ops = transformed_qnode.qtape.operations - compare_operation_lists(ops, expected_op_list, expected_wires_list) + tape = qml.workflow.construct_tape(transformed_qnode)(input) + compare_operation_lists(tape.operations, expected_op_list, expected_wires_list) @pytest.mark.jax def test_single_qubit_fusion_jax_jit(self): @@ -345,5 +336,5 @@ def test_single_qubit_fusion_jax_jit(self): assert qml.math.allclose(jax.grad(jitted_transformed_qnode)(input), original_gradient) # Check operation list - ops = transformed_qnode.qtape.operations - compare_operation_lists(ops, expected_op_list, expected_wires_list) + tape = qml.workflow.construct_tape(transformed_qnode)(input) + compare_operation_lists(tape.operations, expected_op_list, expected_wires_list) diff --git a/tests/transforms/test_optimization/test_undo_swaps.py b/tests/transforms/test_optimization/test_undo_swaps.py index 554f28d26b1..7f92b1c3893 100644 --- a/tests/transforms/test_optimization/test_undo_swaps.py +++ b/tests/transforms/test_optimization/test_undo_swaps.py @@ -14,8 +14,6 @@ """ Unit tests for the optimization transform ``undo_swaps``. """ -import warnings - import pytest from utils import compare_operation_lists @@ -25,13 +23,6 @@ from pennylane.wires import Wires -@pytest.fixture(autouse=True) -def suppress_tape_property_deprecation_warning(): - warnings.filterwarnings( - "ignore", "The tape/qtape property is deprecated", category=qml.PennyLaneDeprecationWarning - ) - - class TestUndoSwaps: """Test that check the main functionalities of the `undo_swaps` transform""" @@ -225,8 +216,8 @@ def test_undo_swaps_autograd(self): ) # Check operation list - ops = transformed_qnode.qtape.operations - compare_operation_lists(ops, expected_op_list, expected_wires_list) + tape = qml.workflow.construct_tape(transformed_qnode)(input) + compare_operation_lists(tape.operations, expected_op_list, expected_wires_list) @pytest.mark.torch def test_undo_swaps_torch(self): @@ -252,8 +243,8 @@ def test_undo_swaps_torch(self): assert qml.math.allclose(original_input.grad, transformed_input.grad) # Check operation list - ops = transformed_qnode.qtape.operations - compare_operation_lists(ops, expected_op_list, expected_wires_list) + tape = qml.workflow.construct_tape(transformed_qnode)(transformed_input) + compare_operation_lists(tape.operations, expected_op_list, expected_wires_list) @pytest.mark.tf def test_undo_swaps_tf(self): @@ -284,8 +275,8 @@ def test_undo_swaps_tf(self): assert qml.math.allclose(original_grad, transformed_grad) # Check operation list - ops = transformed_qnode.qtape.operations - compare_operation_lists(ops, expected_op_list, expected_wires_list) + tape = qml.workflow.construct_tape(transformed_qnode)(transformed_input) + compare_operation_lists(tape.operations, expected_op_list, expected_wires_list) @pytest.mark.jax def test_undo_swaps_jax(self): @@ -307,5 +298,6 @@ def test_undo_swaps_jax(self): ) # Check operation list - ops = transformed_qnode.qtape.operations + tape = qml.workflow.construct_tape(transformed_qnode)(input) + ops = tape.operations compare_operation_lists(ops, expected_op_list, expected_wires_list) diff --git a/tests/transforms/test_qcut.py b/tests/transforms/test_qcut.py index a3f04ebeb31..e3d6d80e0eb 100644 --- a/tests/transforms/test_qcut.py +++ b/tests/transforms/test_qcut.py @@ -21,7 +21,6 @@ import itertools import string import sys -import warnings from functools import partial, reduce from itertools import product from os import environ @@ -40,14 +39,6 @@ from pennylane.queuing import WrappedObj from pennylane.wires import Wires - -@pytest.fixture(autouse=True) -def suppress_tape_property_deprecation_warning(): - warnings.filterwarnings( - "ignore", "The tape/qtape property is deprecated", category=qml.PennyLaneDeprecationWarning - ) - - pytestmark = pytest.mark.qcut I, X, Y, Z = ( diff --git a/tests/transforms/test_transpile.py b/tests/transforms/test_transpile.py index fca172508d5..133dcacc331 100644 --- a/tests/transforms/test_transpile.py +++ b/tests/transforms/test_transpile.py @@ -2,7 +2,6 @@ Unit tests for transpiler function. """ -import warnings from math import isclose import pytest @@ -12,13 +11,6 @@ from pennylane.transforms.transpile import transpile -@pytest.fixture(autouse=True) -def suppress_tape_property_deprecation_warning(): - warnings.filterwarnings( - "ignore", "The tape/qtape property is deprecated", category=qml.PennyLaneDeprecationWarning - ) - - def build_qfunc_probs(wires): def qfunc(x, y, z): qml.Hadamard(wires=wires[0]) @@ -230,8 +222,9 @@ def circuit(param): transpiled_qnode = qml.QNode(transpiled_qfunc, dev) transpiled_expectation = transpiled_qnode(param) - original_ops = list(transpiled_qnode.qtape) - transpiled_ops = list(transpiled_qnode.qtape) + tape = qml.workflow.construct_tape(transpiled_qnode)(param) + original_ops = list(tape) + transpiled_ops = list(tape) qml.assert_equal(transpiled_ops[0], original_ops[0]) qml.assert_equal(transpiled_ops[1], original_ops[1]) @@ -273,8 +266,9 @@ def circuit(param): transpiled_qnode = qml.QNode(transpiled_qfunc, dev) transpiled_expectation = transpiled_qnode(param) - original_ops = list(transpiled_qnode.qtape) - transpiled_ops = list(transpiled_qnode.qtape) + tape = qml.workflow.construct_tape(transpiled_qnode)(param) + original_ops = list(tape) + transpiled_ops = list(tape) qml.assert_equal(transpiled_ops[0], original_ops[0]) qml.assert_equal(transpiled_ops[1], original_ops[1]) diff --git a/tests/transforms/test_unitary_to_rot.py b/tests/transforms/test_unitary_to_rot.py index 37952c9cd6c..7eb8fb53bc6 100644 --- a/tests/transforms/test_unitary_to_rot.py +++ b/tests/transforms/test_unitary_to_rot.py @@ -14,7 +14,6 @@ """ Tests for the QubitUnitary decomposition transforms. """ -import warnings from itertools import product import pytest @@ -26,14 +25,6 @@ from pennylane.transforms import unitary_to_rot from pennylane.wires import Wires - -@pytest.fixture(autouse=True) -def suppress_tape_property_deprecation_warning(): - warnings.filterwarnings( - "ignore", "The tape/qtape property is deprecated", category=qml.PennyLaneDeprecationWarning - ) - - typeof_gates_zyz = (qml.RZ, qml.RY, qml.RZ) single_qubit_decompositions = [ (I, typeof_gates_zyz, [0.0, 0.0, 0.0]), @@ -569,7 +560,8 @@ def two_qubit_decomp_qnode(x, y, z): assert qml.math.allclose(original_qnode(x, y, z), transformed_qnode(x, y, z)) # 3 normal operations + 18 for the first decomp and 6 for the second - assert len(transformed_qnode.qtape.operations) == 27 + tape = qml.workflow.construct_tape(transformed_qnode)(x, y, z) + assert len(tape.operations) == 27 original_grad = qml.grad(original_qnode)(x, y, z) transformed_grad = qml.grad(transformed_qnode)(x, y, z) @@ -617,7 +609,10 @@ def two_qubit_decomp_qnode(x, y, z): assert qml.math.allclose(original_result, transformed_result) - assert len(transformed_qnode.qtape.operations) == 27 + tape = qml.workflow.construct_tape(transformed_qnode)( + transformed_x, transformed_y, transformed_z + ) + assert len(tape.operations) == 27 original_result.backward() transformed_result.backward() @@ -662,7 +657,8 @@ def two_qubit_decomp_qnode(x): assert qml.math.allclose(original_result, transformed_result) - assert len(transformed_qnode.qtape.operations) == 25 + tape = qml.workflow.construct_tape(transformed_qnode)(transformed_x) + assert len(tape.operations) == 25 with tf.GradientTape() as tape: loss = original_qnode(x) @@ -707,7 +703,8 @@ def two_qubit_decomp_qnode(x): assert qml.math.allclose(original_qnode(x), transformed_qnode(x)) # 1 normal operations + 18 for the first decomp and 6 for the second - assert len(transformed_qnode.qtape.operations) == 25 + tape = qml.workflow.construct_tape(transformed_qnode)(x) + assert len(tape.operations) == 25 original_grad = jax.grad(original_qnode, argnums=(0))(x) transformed_grad = jax.grad(transformed_qnode, argnums=(0))(x) diff --git a/tests/workflow/interfaces/qnode/test_autograd_qnode.py b/tests/workflow/interfaces/qnode/test_autograd_qnode.py index 573d8d13999..8de9f219d5d 100644 --- a/tests/workflow/interfaces/qnode/test_autograd_qnode.py +++ b/tests/workflow/interfaces/qnode/test_autograd_qnode.py @@ -14,7 +14,6 @@ """Integration tests for using the autograd interface with a QNode""" # pylint: disable=no-member, too-many-arguments, unexpected-keyword-arg, use-dict-literal, no-name-in-module -import warnings import autograd import autograd.numpy as anp @@ -26,14 +25,6 @@ from pennylane import qnode from pennylane.devices import DefaultQubit - -@pytest.fixture(autouse=True) -def suppress_tape_property_deprecation_warning(): - warnings.filterwarnings( - "ignore", "The tape/qtape property is deprecated", category=qml.PennyLaneDeprecationWarning - ) - - # dev, diff_method, grad_on_execution, device_vjp qubit_device_and_diff_method = [ [qml.device("default.qubit"), "finite-diff", False, False], @@ -165,7 +156,6 @@ def circuit(a, b): def cost(x, y): return autograd.numpy.hstack(circuit(x, y)) - assert circuit.qtape.trainable_params == [0, 1] assert isinstance(res, tuple) assert len(res) == 2 @@ -276,10 +266,6 @@ def loss(a, b): grad_fn = qml.grad(loss) res = grad_fn(a, b) - - # the tape has reported both arguments as trainable - assert circuit.qtape.trainable_params == [0, 1] - expected = [-np.sin(a) + np.sin(a) * np.sin(b), -np.cos(a) * np.cos(b)] assert np.allclose(res, expected, atol=tol, rtol=0) @@ -288,18 +274,15 @@ def loss(a, b): b = np.array(0.8, requires_grad=False) res = grad_fn(a, b) - - # the tape has reported only the first argument as trainable - assert circuit.qtape.trainable_params == [0] - expected = [-np.sin(a) + np.sin(a) * np.sin(b)] assert np.allclose(res, expected, atol=tol, rtol=0) # trainability also updates on evaluation a = np.array(0.54, requires_grad=False) b = np.array(0.8, requires_grad=True) - circuit(a, b) - assert circuit.qtape.trainable_params == [1] + res = grad_fn(a, b) + expected = [-np.cos(a) * np.cos(b)] + assert np.allclose(res, expected, atol=tol, rtol=0) def test_classical_processing(self, interface, dev, diff_method, grad_on_execution, device_vjp): """Test classical processing within the quantum tape""" @@ -322,10 +305,6 @@ def circuit(a, b, c): res = qml.jacobian(circuit)(a, b, c) - assert circuit.qtape.trainable_params == [0, 2] - tape_params = np.array(circuit.qtape.get_parameters()) - assert np.all(tape_params == [a * c, c + c**2 + np.sin(a)]) - assert isinstance(res, tuple) and len(res) == 2 assert res[0].shape == () assert res[1].shape == () @@ -353,9 +332,6 @@ def circuit(a, b): res = circuit(a, b) - if diff_method == "finite-diff": - assert circuit.qtape.trainable_params == [] - assert len(res) == 2 assert isinstance(res, tuple) @@ -395,9 +371,6 @@ def circuit(U, a): res = circuit(U, a) - if diff_method == "finite-diff": - assert circuit.qtape.trainable_params == [1] - res = qml.grad(circuit)(U, a) assert np.allclose(res, np.sin(a), atol=tol, rtol=0) @@ -933,15 +906,6 @@ def cost(a, b, c, weights): # to be compared with the expected result assert np.allclose(np.hstack(res), expected, atol=tol, rtol=0) - if diff_method != "backprop": - # Check that the gradient was computed - # for all parameters in circuit2 - assert circuit2.qtape.trainable_params == [0, 1, 2, 3] - - # Check that the parameter-shift rule was not applied - # to the first parameter of circuit1. - assert circuit1.qtape.trainable_params == [1, 2] - def test_second_derivative( self, interface, dev, diff_method, grad_on_execution, device_vjp, tol ): diff --git a/tests/workflow/interfaces/qnode/test_jax_jit_qnode.py b/tests/workflow/interfaces/qnode/test_jax_jit_qnode.py index ae6d663f828..8d1e17e8f31 100644 --- a/tests/workflow/interfaces/qnode/test_jax_jit_qnode.py +++ b/tests/workflow/interfaces/qnode/test_jax_jit_qnode.py @@ -13,7 +13,6 @@ # limitations under the License. """Integration tests for using the JAX-JIT interface with a QNode""" -import warnings # pylint: disable=too-many-arguments,too-few-public-methods,protected-access from functools import partial @@ -27,13 +26,6 @@ from pennylane.devices import DefaultQubit -@pytest.fixture(autouse=True) -def suppress_tape_property_deprecation_warning(): - warnings.filterwarnings( - "ignore", "The tape/qtape property is deprecated", category=qml.PennyLaneDeprecationWarning - ) - - def get_device(device_name, wires, seed): if device_name == "param_shift.qubit": return ParamShiftDerivativesDevice(seed=seed) @@ -106,9 +98,6 @@ def circuit(a): assert circuit.interface == interface - # the tape is able to deduce trainable parameters - assert circuit.qtape.trainable_params == [0] - # gradients should work grad = jax.jit(jax.grad(circuit))(a) assert isinstance(grad, jax.Array) @@ -142,28 +131,16 @@ def circuit(a, b): grad_fn = jax.jit(jax.grad(circuit, argnums=[0, 1])) res = grad_fn(a, b) - # the tape has reported both arguments as trainable - assert circuit.qtape.trainable_params == [0, 1] - expected = [-np.sin(a) + np.sin(a) * np.sin(b), -np.cos(a) * np.cos(b)] assert np.allclose(res, expected, atol=tol, rtol=0) # make the second QNode argument a constant - grad_fn = jax.grad(circuit, argnums=0) + grad_fn = jax.jit(jax.grad(circuit, argnums=0)) res = grad_fn(a, b) - # the tape has reported only the first argument as trainable - assert circuit.qtape.trainable_params == [0] - expected = [-np.sin(a) + np.sin(a) * np.sin(b)] assert np.allclose(res, expected, atol=tol, rtol=0) - # trainability also updates on evaluation - a = np.array(0.54, requires_grad=False) - b = np.array(0.8, requires_grad=True) - circuit(a, b) - assert circuit.qtape.trainable_params == [1] - def test_classical_processing( self, dev_name, diff_method, grad_on_execution, device_vjp, interface, seed ): @@ -189,10 +166,7 @@ def circuit(a, b, c): qml.RX(c + c**2 + jax.numpy.sin(a), wires=0) return qml.expval(qml.PauliZ(0)) - res = jax.grad(circuit, argnums=[0, 2])(a, b, c) - - if diff_method == "finite-diff": - assert circuit.qtape.trainable_params == [0, 2] + res = jax.jit(jax.grad(circuit, argnums=[0, 2]))(a, b, c) assert len(res) == 2 @@ -220,12 +194,9 @@ def circuit(U, a): qml.RY(a, wires=0) return qml.expval(qml.PauliZ(0)) - res = jax.grad(circuit, argnums=1)(U, a) + res = jax.jit(jax.grad(circuit, argnums=1))(U, a) assert np.allclose(res, np.sin(a), atol=tol, rtol=0) - if diff_method == "finite-diff": - assert circuit.qtape.trainable_params == [1] - def test_differentiable_expand( self, dev_name, diff_method, grad_on_execution, device_vjp, interface, tol, seed ): @@ -362,7 +333,6 @@ def circuit(a, b): res = jax.jit(circuit)(a, b) - assert circuit.qtape.trainable_params == [0, 1] assert isinstance(res, tuple) assert len(res) == 2 @@ -371,7 +341,6 @@ def circuit(a, b): assert np.allclose(res[1], expected[1], atol=tol, rtol=0) res = jax.jit(jax.jacobian(circuit, argnums=[0, 1]))(a, b) - assert circuit.qtape.trainable_params == [0, 1] expected = np.array([[-np.sin(a), 0], [np.sin(a) * np.sin(b), -np.cos(a) * np.cos(b)]]) assert isinstance(res, tuple) diff --git a/tests/workflow/interfaces/qnode/test_jax_qnode.py b/tests/workflow/interfaces/qnode/test_jax_qnode.py index 6fe0e2dc16b..325007d062d 100644 --- a/tests/workflow/interfaces/qnode/test_jax_qnode.py +++ b/tests/workflow/interfaces/qnode/test_jax_qnode.py @@ -14,7 +14,6 @@ """Integration tests for using the JAX-Python interface with a QNode""" # pylint: disable=no-member, too-many-arguments, unexpected-keyword-arg, use-implicit-booleaness-not-comparison -import warnings from itertools import product import numpy as np @@ -25,13 +24,6 @@ from pennylane.devices import DefaultQubit -@pytest.fixture(autouse=True) -def suppress_tape_property_deprecation_warning(): - warnings.filterwarnings( - "ignore", "The tape/qtape property is deprecated", category=qml.PennyLaneDeprecationWarning - ) - - def get_device(device_name, wires, seed): if device_name == "lightning.qubit": return qml.device("lightning.qubit", wires=wires) @@ -97,14 +89,9 @@ def circuit(a): assert circuit.interface == interface - # jax doesn't set trainable parameters on regular execution - assert circuit.qtape.trainable_params == [] - # gradients should work grad = jax.grad(circuit)(a) assert isinstance(grad, jax.Array) - # the tape is able to deduce trainable parameters - assert circuit.qtape.trainable_params == [0] assert grad.shape == () def test_changing_trainability( @@ -132,9 +119,6 @@ def circuit(a, b): grad_fn = jax.grad(circuit, argnums=[0, 1]) res = grad_fn(a, b) - # the tape has reported both arguments as trainable - assert circuit.qtape.trainable_params == [0, 1] - expected = [-np.sin(a) + np.sin(a) * np.sin(b), -np.cos(a) * np.cos(b)] assert np.allclose(res, expected, atol=tol, rtol=0) @@ -142,9 +126,6 @@ def circuit(a, b): grad_fn = jax.grad(circuit, argnums=0) res = grad_fn(a, b) - # the tape has reported only the first argument as trainable - assert circuit.qtape.trainable_params == [0] - expected = [-np.sin(a) + np.sin(a) * np.sin(b)] assert np.allclose(res, expected, atol=tol, rtol=0) @@ -171,9 +152,6 @@ def circuit(a, b, c): res = jax.grad(circuit, argnums=[0, 2])(a, b, c) - if diff_method == "finite-diff": - assert circuit.qtape.trainable_params == [0, 2] - assert len(res) == 2 def test_matrix_parameter( @@ -199,9 +177,6 @@ def circuit(U, a): res = jax.grad(circuit, argnums=1)(U, a) assert np.allclose(res, np.sin(a), atol=tol, rtol=0) - if diff_method == "finite-diff": - assert circuit.qtape.trainable_params == [1] - def test_differentiable_expand( self, dev_name, diff_method, grad_on_execution, interface, device_vjp, tol, seed ): @@ -317,7 +292,6 @@ def circuit(a, b): res = circuit(a, b) - assert circuit.qtape.trainable_params == [] assert isinstance(res, tuple) assert len(res) == 2 @@ -327,7 +301,6 @@ def circuit(a, b): res = jax.jacobian(circuit, argnums=[0, 1])(a, b) expected = np.array([[-np.sin(a), 0], [np.sin(a) * np.sin(b), -np.cos(a) * np.cos(b)]]) - assert circuit.qtape.trainable_params == [0, 1] assert isinstance(res, tuple) assert len(res) == 2 diff --git a/tests/workflow/interfaces/qnode/test_tensorflow_qnode.py b/tests/workflow/interfaces/qnode/test_tensorflow_qnode.py index e8441f7f61b..3ac2404c244 100644 --- a/tests/workflow/interfaces/qnode/test_tensorflow_qnode.py +++ b/tests/workflow/interfaces/qnode/test_tensorflow_qnode.py @@ -12,7 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. """Integration tests for using the TensorFlow interface with a QNode""" -import warnings import numpy as np @@ -23,14 +22,6 @@ from pennylane import qnode from pennylane.devices import DefaultQubit - -@pytest.fixture(autouse=True) -def suppress_tape_property_deprecation_warning(): - warnings.filterwarnings( - "ignore", "The tape/qtape property is deprecated", category=qml.PennyLaneDeprecationWarning - ) - - pytestmark = pytest.mark.tf tf = pytest.importorskip("tensorflow") @@ -88,10 +79,6 @@ def circuit(a): a = tf.Variable(0.1) circuit(a) - # if executing outside a gradient tape, the number of trainable parameters - # cannot be determined by TensorFlow - assert circuit.qtape.trainable_params == [] - with tf.GradientTape() as tape: res = circuit(a) @@ -101,9 +88,6 @@ def circuit(a): assert isinstance(res, tf.Tensor) assert res.shape == () - # the tape is able to deduce trainable parameters - assert circuit.qtape.trainable_params == [0] - # gradients should work grad = tape.gradient(res, a) assert isinstance(grad, tf.Tensor) @@ -199,8 +183,6 @@ def circuit(a, b): res = circuit(a, b) res = tf.stack(res) - assert circuit.qtape.trainable_params == [0, 1] - assert isinstance(res, tf.Tensor) assert res.shape == (2,) @@ -270,9 +252,6 @@ def circuit(a, b): res = circuit(a, b) res = tf.stack(res) - # the tape has reported both gate arguments as trainable - assert circuit.qtape.trainable_params == [0, 1] - expected = [tf.cos(a), -tf.cos(a) * tf.sin(b)] assert np.allclose(res, expected, atol=tol, rtol=0) @@ -291,9 +270,6 @@ def circuit(a, b): res = circuit(a, b) res = tf.stack(res) - # the tape has reported only the first argument as trainable - assert circuit.qtape.trainable_params == [0] - expected = [tf.cos(a), -tf.cos(a) * tf.sin(b)] assert np.allclose(res, expected, atol=tol, rtol=0) @@ -323,10 +299,6 @@ def circuit(x, y, z): with tf.GradientTape(persistent=device_vjp) as tape: res = circuit(a, b, c) - if diff_method == "finite-diff": - assert circuit.qtape.trainable_params == [0, 2] - assert circuit.qtape.get_parameters() == [a * c, c + c**2 + tf.sin(a)] - res = tape.jacobian(res, [a, b, c], experimental_use_pfor=not device_vjp) assert isinstance(res[0], tf.Tensor) @@ -358,9 +330,6 @@ def circuit(a, b): res = circuit(a, b) res = tf.stack(res) - if diff_method == "finite-diff": - assert circuit.qtape.trainable_params == [] - assert res.shape == (2,) assert isinstance(res, tf.Tensor) @@ -391,9 +360,6 @@ def circuit(U, a): with tf.GradientTape(persistent=device_vjp) as tape: res = circuit(U, a) - if diff_method == "finite-diff": - assert circuit.qtape.trainable_params == [1] - assert np.allclose(res, -tf.cos(a), atol=tol, rtol=0) res = tape.jacobian(res, a, experimental_use_pfor=not device_vjp) diff --git a/tests/workflow/interfaces/qnode/test_torch_qnode.py b/tests/workflow/interfaces/qnode/test_torch_qnode.py index fe33abedc7d..8621e29ab31 100644 --- a/tests/workflow/interfaces/qnode/test_torch_qnode.py +++ b/tests/workflow/interfaces/qnode/test_torch_qnode.py @@ -12,7 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. """Integration tests for using the Torch interface with a QNode""" -import warnings # pylint: disable=too-many-arguments,unexpected-keyword-arg,no-member,comparison-with-callable, no-name-in-module # pylint: disable=use-implicit-booleaness-not-comparison, unnecessary-lambda-assignment, use-dict-literal @@ -24,14 +23,6 @@ from pennylane import qnode from pennylane.devices import DefaultQubit - -@pytest.fixture(autouse=True) -def suppress_tape_property_deprecation_warning(): - warnings.filterwarnings( - "ignore", "The tape/qtape property is deprecated", category=qml.PennyLaneDeprecationWarning - ) - - pytestmark = pytest.mark.torch torch = pytest.importorskip("torch", minversion="1.3") @@ -104,9 +95,6 @@ def circuit(a): assert isinstance(res, torch.Tensor) assert res.shape == () - # the tape is able to deduce trainable parameters - assert circuit.qtape.trainable_params == [0] - # gradients should work res.backward() grad = a.grad @@ -205,8 +193,6 @@ def circuit(a, b): res = circuit(a, b) - assert circuit.qtape.trainable_params == [0, 1] - assert isinstance(res, tuple) assert len(res) == 2 @@ -264,7 +250,6 @@ def circuit(a, b): res = circuit(a, b) assert circuit.interface == interface - assert circuit.qtape.trainable_params == [0, 1] assert isinstance(res, tuple) assert len(res) == 2 @@ -337,9 +322,6 @@ def circuit(a, b): res = circuit(a, b) - # the tape has reported both gate arguments as trainable - assert circuit.qtape.trainable_params == [0, 1] - expected = [np.cos(a_val), -np.cos(a_val) * np.sin(b_val)] assert np.allclose(res[0].detach().numpy(), expected[0], atol=tol, rtol=0) @@ -363,9 +345,6 @@ def circuit(a, b): res = circuit(a, b) - # the tape has reported only the first argument as trainable - assert circuit.qtape.trainable_params == [0] - expected = [np.cos(a_val), -np.cos(a_val) * np.sin(b_val)] assert np.allclose(res[0].detach().numpy(), expected[0], atol=tol, rtol=0) @@ -404,10 +383,6 @@ def circuit(a, b, c): res = circuit(a, b, c) - if diff_method == "finite-diff": - assert circuit.qtape.trainable_params == [0, 2] - assert circuit.qtape.get_parameters() == [a * c, c + c**2 + torch.sin(a)] - res.backward() assert isinstance(a.grad, torch.Tensor) @@ -442,9 +417,6 @@ def circuit(a, b): res = circuit(a, b) - if diff_method == "finite-diff": - assert circuit.qtape.trainable_params == [] - assert isinstance(res, tuple) assert len(res) == 2 @@ -495,9 +467,6 @@ def circuit(U, a): res = circuit(U, a) - if diff_method == "finite-diff": - assert circuit.qtape.trainable_params == [1] - assert np.allclose(res.detach(), -np.cos(a_val), atol=tol, rtol=0) res.backward()