def test_evaluate_circuit_hash_hermitian_different_matrices(self, x, y): """Tests that the circuit hashes of identical circuits except for the matrix argument of the Hermitian observable in the return statement are different.""" dev = qml.device("default.qubit", wires=3) matrix_1 = np.array([[1, 0], [0, 1]]) matrix_2 = np.array([[1, 0], [0, -1]]) def circuit1(x, y): qml.Rot(x, y, 0.3, wires=[0]) qml.CNOT(wires=[0, 1]) return qml.expval( qml.Hermitian(matrix_1, wires=[0]) @ qml.PauliX(1)) node1 = BaseQNode(circuit1, dev) node1.evaluate([x, y], {}) circuit_hash_1 = node1.circuit.hash def circuit2(x, y): qml.Rot(x, y, 0.3, wires=[0]) qml.CNOT(wires=[0, 1]) return qml.expval( qml.Hermitian(matrix_2, wires=[0]) @ qml.PauliX(1)) node2 = BaseQNode(circuit2, dev) node2.evaluate([x, y], {}) circuit_hash_2 = node2.circuit.hash assert circuit_hash_1 != circuit_hash_2
def test_evaluate_circuit_hash_same_operation_has_numeric_and_symbolic_different_wires_in_return( self, x, y): """Tests that the circuit hashes of identical circuits except for the wires in the return statement are different.""" dev = qml.device("default.qubit", wires=3) def circuit1(x, y): qml.Rot(x, y, 0.3, wires=[0]) qml.CNOT(wires=[0, 1]) return qml.expval( qml.PauliZ(0) @ qml.PauliX(1)) # <----- (0) @ (1) node1 = BaseQNode(circuit1, dev) node1.evaluate([x, y], {}) circuit_hash_1 = node1.circuit.hash def circuit2(x, y): qml.Rot(x, y, 0.3, wires=[0]) qml.CNOT(wires=[0, 1]) return qml.expval( qml.PauliZ(0) @ qml.PauliX(2)) # <----- (0) @ (2) node2 = BaseQNode(circuit2, dev) node2.evaluate([x, y], {}) circuit_hash_2 = node2.circuit.hash assert circuit_hash_1 != circuit_hash_2
def test_evaluate_circuit_hash_numeric_and_symbolic_different_parameter( self, x, y): """Tests that the circuit hashes of identical circuits except for the numeric argument of a signle operation in the circuits are different""" dev = qml.device("default.qubit", wires=3) def circuit1(x, y): qml.RX(x, wires=[0]) qml.RY(y, wires=[1]) qml.RZ(0.3, wires=[2]) # <------------- 0.3 qml.CNOT(wires=[0, 1]) return qml.expval(qml.PauliZ(0) @ qml.PauliX(1)) node1 = BaseQNode(circuit1, dev) node1.evaluate([x, y], {}) circuit_hash_1 = node1.circuit.hash def circuit2(x, y): qml.RX(x, wires=[0]) qml.RY(y, wires=[1]) qml.RZ(0.5, wires=[2]) # <------------- 0.5 qml.CNOT(wires=[0, 1]) return qml.expval(qml.PauliZ(0) @ qml.PauliX(1)) node2 = BaseQNode(circuit2, dev) node2.evaluate([x, y], {}) circuit_hash_2 = node2.circuit.hash assert circuit_hash_1 != circuit_hash_2
def test_evaluate_circuit_hash_different_return_observable_vs_tensor( self, x, y): """Tests that the circuit hashes of identical circuits except for the return statement are different""" dev = qml.device("default.qubit", wires=3) def circuit1(x, y): qml.RX(x, wires=[0]) qml.RY(y, wires=[1]) qml.RZ(0.3, wires=[2]) qml.CNOT(wires=[0, 1]) return qml.expval(qml.PauliZ(0)) # <------------- qml.PauliZ(0) node1 = BaseQNode(circuit1, dev) node1.evaluate([x, y], {}) circuit_hash_1 = node1.circuit.hash def circuit2(x, y): qml.RX(x, wires=[0]) qml.RY(y, wires=[1]) qml.RZ(0.3, wires=[2]) qml.CNOT(wires=[0, 1]) return qml.expval(qml.PauliZ(0) @ qml.PauliX(1) ) # <------------- qml.PauliZ(0) @ qml.PauliX(1) node2 = BaseQNode(circuit2, dev) node2.evaluate([x, y], {}) circuit_hash_2 = node2.circuit.hash assert circuit_hash_1 != circuit_hash_2
def test_evaluate_circuit_hash_same_operation_has_numeric_and_symbolic_different_order( self, x, y): """Tests that the circuit hashes of identical circuits except for the order of numeric and symbolic arguments in one of the operations are different.""" dev = qml.device("default.qubit", wires=3) def circuit1(x, y): qml.Rot(x, 0.3, y, wires=[0]) # <------------- x, 0.3, y qml.CNOT(wires=[0, 1]) return qml.expval(qml.PauliZ(0) @ qml.PauliX(1)) node1 = BaseQNode(circuit1, dev) node1.evaluate([x, y], {}) circuit_hash_1 = node1.circuit.hash def circuit2(x, y): qml.Rot(x, y, 0.3, wires=[0]) # <------------- x, y, 0.3 qml.CNOT(wires=[0, 1]) return qml.expval(qml.PauliZ(0) @ qml.PauliX(1)) node2 = BaseQNode(circuit2, dev) node2.evaluate([x, y], {}) circuit_hash_2 = node2.circuit.hash assert circuit_hash_1 != circuit_hash_2
def test_evaluate_circuit_hash_numeric_different(self): """Tests that the circuit hashes of identical circuits except for one numeric value are different""" dev = qml.device("default.qubit", wires=2) a = 0.3 b = 0.2 def circuit1(): qml.RX(a, wires=[0]) qml.RY(b, wires=[1]) qml.CNOT(wires=[0, 1]) return qml.expval(qml.PauliZ(0) @ qml.PauliX(1)) node1 = BaseQNode(circuit1, dev) node1.evaluate([], {}) circuit_hash_1 = node1.circuit.hash c = 0.6 def circuit2(): qml.RX(c, wires=[0]) qml.RY(b, wires=[1]) qml.CNOT(wires=[0, 1]) return qml.expval(qml.PauliZ(0) @ qml.PauliX(1)) node2 = BaseQNode(circuit2, dev) node2.evaluate([], {}) circuit_hash_2 = node2.circuit.hash assert circuit_hash_1 != circuit_hash_2
def test_evaluate_circuit_hash_same_operation_has_numeric_and_symbolic( self, x, y): """Tests that the circuit hashes of identical circuits where one operation has both numeric and symbolic arguments are equal""" dev = qml.device("default.qubit", wires=3) def circuit1(x, y): qml.Rot(x, y, 0.3, wires=[0]) qml.CNOT(wires=[0, 1]) return qml.expval(qml.PauliZ(0) @ qml.PauliX(1)) node1 = BaseQNode(circuit1, dev) node1.evaluate([x, y], {}) circuit_hash_1 = node1.circuit.hash def circuit2(x, y): qml.Rot(x, y, 0.3, wires=[0]) qml.CNOT(wires=[0, 1]) return qml.expval(qml.PauliZ(0) @ qml.PauliX(1)) node2 = BaseQNode(circuit2, dev) node2.evaluate([x, y], {}) circuit_hash_2 = node2.circuit.hash assert circuit_hash_1 == circuit_hash_2
def test_evaluate_circuit_hash_hermitian(self, x, y): """Tests that the circuit hashes of identical circuits containing a Hermitian observable are equal""" dev = qml.device("default.qubit", wires=3) matrix = np.array([[1, 0], [0, 1]]) def circuit1(x, y): qml.Rot(x, y, 0.3, wires=[0]) qml.CNOT(wires=[0, 1]) return qml.expval(qml.Hermitian(matrix, wires=[0]) @ qml.PauliX(1)) node1 = BaseQNode(circuit1, dev) node1.evaluate([x, y], {}) circuit_hash_1 = node1.circuit.hash def circuit2(x, y): qml.Rot(x, y, 0.3, wires=[0]) qml.CNOT(wires=[0, 1]) return qml.expval(qml.Hermitian(matrix, wires=[0]) @ qml.PauliX(1)) node2 = BaseQNode(circuit2, dev) node2.evaluate([x, y], {}) circuit_hash_2 = node2.circuit.hash assert circuit_hash_1 == circuit_hash_2
def test_evaluate_circuit_hash_numeric_and_symbolic_tensor_return( self, x, y): """Tests that the circuit hashes of identical circuits having a tensor product in the return statement are equal""" dev = qml.device("default.qubit", wires=3) def circuit1(x, y): qml.RX(x, wires=[0]) qml.RY(y, wires=[1]) qml.RZ(0.3, wires=[2]) qml.CNOT(wires=[0, 1]) return qml.expval(qml.PauliZ(0)) node1 = BaseQNode(circuit1, dev) node1.evaluate([x, y], {}) circuit_hash_1 = node1.circuit.hash def circuit2(x, y): qml.RX(x, wires=[0]) qml.RY(y, wires=[1]) qml.RZ(0.3, wires=[2]) qml.CNOT(wires=[0, 1]) return qml.expval(qml.PauliZ(0)) node2 = BaseQNode(circuit2, dev) node2.evaluate([x, y], {}) circuit_hash_2 = node2.circuit.hash assert circuit_hash_1 == circuit_hash_2
def test_evaluate_circuit_hash_symbolic_assigned_arguments_do_not_matter( self, a, b, x, y): """Tests that the circuit hashes of identical circuits where different values are assigned to symbolic parameters are equal""" dev = qml.device("default.qubit", wires=2) def circuit1(a, b): qml.RX(a, wires=[0]) qml.RY(b, wires=[1]) qml.CNOT(wires=[0, 1]) return qml.expval(qml.PauliZ(0)) node1 = BaseQNode(circuit1, dev) node1.evaluate([a, b], {}) circuit_hash_1 = node1.circuit.hash def circuit2(x, y): qml.RX(x, wires=[0]) qml.RY(y, wires=[1]) qml.CNOT(wires=[0, 1]) return qml.expval(qml.PauliZ(0)) node2 = BaseQNode(circuit2, dev) node2.evaluate([x, y], {}) circuit_hash_2 = node2.circuit.hash assert circuit_hash_1 == circuit_hash_2
def test_evaluate_circuit_hash_numeric_and_symbolic(self, x, y): """Tests that the circuit hash of identical circuits containing numeric and symbolic parameters are equal""" dev = qml.device("default.qubit", wires=3) def circuit1(x, y): qml.RX(x, wires=[0]) qml.RY(y, wires=[1]) qml.RZ(0.3, wires=[2]) qml.CNOT(wires=[0, 1]) return qml.expval(qml.PauliZ(0)) node1 = BaseQNode(circuit1, dev) node1.evaluate([x, y], {}) circuit_hash_1 = node1.circuit.hash def circuit2(x, y): qml.RX(x, wires=[0]) qml.RY(y, wires=[1]) qml.RZ(0.3, wires=[2]) qml.CNOT(wires=[0, 1]) return qml.expval(qml.PauliZ(0)) node2 = BaseQNode(circuit2, dev) node2.evaluate([x, y], {}) circuit_hash_2 = node2.circuit.hash assert circuit_hash_1 == circuit_hash_2
def test_evaluate_circuit_hash_numeric(self): """Tests that the circuit hash of identical circuits containing only numeric parameters are equal""" dev = qml.device("default.qubit", wires=2) a = 0.3 b = 0.2 def circuit1(): qml.RX(a, wires=[0]) qml.RY(b, wires=[1]) qml.CNOT(wires=[0, 1]) return qml.expval(qml.PauliZ(0)) node1 = BaseQNode(circuit1, dev) node1.evaluate([], {}) circuit_hash_1 = node1.circuit.hash def circuit2(): qml.RX(a, wires=[0]) qml.RY(b, wires=[1]) qml.CNOT(wires=[0, 1]) return qml.expval(qml.PauliZ(0)) node2 = BaseQNode(circuit2, dev) node2.evaluate([], {}) circuit_hash_2 = node2.circuit.hash assert circuit_hash_1 == circuit_hash_2
def test_evaluate_circuit_hash_numeric_and_symbolic_return_type_does_not_matter( self, x, y): """Tests that the circuit hashes of identical circuits only differing on their return types are equal""" dev = qml.device("default.qubit", wires=3) def circuit1(x, y): qml.Rot(x, y, 0.3, wires=[0]) qml.CNOT(wires=[0, 1]) return qml.expval(qml.PauliZ(0) @ qml.PauliX(1)) node1 = BaseQNode(circuit1, dev) node1.evaluate([x, y], {}) circuit_hash_1 = node1.circuit.hash def circuit2(x, y): qml.Rot(x, y, 0.3, wires=[0]) qml.CNOT(wires=[0, 1]) return qml.var(qml.PauliZ(0) @ qml.PauliX(1)) node2 = BaseQNode(circuit2, dev) node2.evaluate([x, y], {}) circuit_hash_2 = node2.circuit.hash def circuit3(x, y): qml.Rot(x, y, 0.3, wires=[0]) qml.CNOT(wires=[0, 1]) return qml.sample(qml.PauliZ(0) @ qml.PauliX(1)) node3 = BaseQNode(circuit1, dev) node3.evaluate([x, y], {}) circuit_hash_3 = node3.circuit.hash assert circuit_hash_1 == circuit_hash_2 == circuit_hash_3
def test_evaluate_circuit_hash_numeric_different_operation(self): """Tests that the circuit hashes of identical circuits except for one of the operations are different""" dev = qml.device("default.qubit", wires=2) a = 0.3 def circuit1(): qml.RX(a, wires=[0]) return qml.expval(qml.PauliZ(0)) node1 = BaseQNode(circuit1, dev) node1.evaluate([], {}) circuit_hash_1 = node1.circuit.hash def circuit2(): qml.RY(a, wires=[0]) return qml.expval(qml.PauliZ(0)) node2 = BaseQNode(circuit2, dev) node2.evaluate([], {}) circuit_hash_2 = node2.circuit.hash assert circuit_hash_1 != circuit_hash_2