def test_multiple_expectation_different_wires(self, qubit_device_2_wires, tol): """Tests that qnodes return multiple expectation values.""" a, b, c = torch.tensor(0.5), torch.tensor(0.54), torch.tensor(0.3) @qml.qnode(qubit_device_2_wires, interface='torch') def circuit(x, y, z): qml.RX(x, wires=[0]) qml.RZ(y, wires=[0]) qml.CNOT(wires=[0, 1]) qml.RY(y, wires=[0]) qml.RX(z, wires=[0]) return qml.expval(qml.PauliY(0)), qml.expval(qml.PauliZ(1)) res = circuit(a, b, c) out_state = np.kron(Rotx(c.numpy()), I) @ np.kron(Roty(b.numpy()), I) @ CNOT \ @ np.kron(Rotz(b.numpy()), I) @ np.kron(Rotx(a.numpy()), I) @ np.array([1, 0, 0, 0]) ex0 = np.vdot(out_state, np.kron(Y, I) @ out_state) ex1 = np.vdot(out_state, np.kron(I, Z) @ out_state) ex = np.array([ex0, ex1]) assert np.allclose(ex, res.numpy(), atol=tol, rtol=0)
def test_qnode_fanout(self, qubit_device_1_wire, tol): """Tests that qnodes can compute the correct function when the same parameter is used in multiple gates.""" @qml.qnode(qubit_device_1_wire, interface='torch') def circuit(reused_param, other_param): qml.RX(reused_param, wires=[0]) qml.RZ(other_param, wires=[0]) qml.RX(reused_param, wires=[0]) return qml.expval(qml.PauliZ(0)) thetas = torch.linspace(-2*np.pi, 2*np.pi, 7) for reused_param in thetas: for theta in thetas: other_param = theta ** 2 / 11 y_eval = circuit(reused_param, other_param) Rx = Rotx(reused_param.numpy()) Rz = Rotz(other_param.numpy()) zero_state = np.array([1.,0.]) final_state = (Rx @ Rz @ Rx @ zero_state) y_true = expZ(final_state) assert np.allclose(y_eval, y_true, atol=tol, rtol=0)
def test_qnode_fanout(self): """Tests that qnodes can compute the correct function when the same parameter is used in multiple gates.""" self.logTestName() @qml.qnode(self.dev1, interface='tfe') def circuit(reused_param, other_param): qml.RX(reused_param, wires=[0]) qml.RZ(other_param, wires=[0]) qml.RX(reused_param, wires=[0]) return qml.expval(qml.PauliZ(0)) thetas = tf.linspace(-2*np.pi, 2*np.pi, 7) for reused_param in thetas: for theta in thetas: other_param = theta ** 2 / 11 y_eval = circuit(reused_param, other_param) Rx = Rotx(reused_param.numpy()) Rz = Rotz(other_param.numpy()) zero_state = np.array([1.,0.]) final_state = (Rx @ Rz @ Rx @ zero_state) y_true = expZ(final_state) self.assertAlmostEqual(y_eval, y_true, delta=self.tol)
def test_multiple_expectation_different_wires(self): "Tests that qnodes return multiple expectation values." self.logTestName() a, b, c = 0.5, 0.54, 0.3 @qml.qnode(self.dev2) def circuit(x, y, z): qml.RX(x, [0]) qml.RZ(y, [0]) qml.CNOT([0, 1]) qml.RY(y, [0]) qml.RX(z, [0]) return qml.expval.PauliY(0), qml.expval.PauliZ(1) res = circuit(a, b, c) out_state = np.kron(Rotx(c), I) @ np.kron(Roty(b), I) @ CNOT \ @ np.kron(Rotz(b), I) @ np.kron(Rotx(a), I) @ np.array([1, 0, 0, 0]) ex0 = np.vdot(out_state, np.kron(Y, I) @ out_state) ex1 = np.vdot(out_state, np.kron(I, Z) @ out_state) ex = np.array([ex0, ex1]) self.assertAllAlmostEqual(ex, res, delta=self.tol)
def test_multiple_expectation_different_wires(self): "Tests that qnodes return multiple expectation values." self.logTestName() a, b, c = tf.constant(0.5), tf.constant(0.54), tf.constant(0.3) @qml.qnode(self.dev2, interface='tfe') def circuit(x, y, z): qml.RX(x, wires=[0]) qml.RZ(y, wires=[0]) qml.CNOT(wires=[0, 1]) qml.RY(y, wires=[0]) qml.RX(z, wires=[0]) return qml.expval(qml.PauliY(0)), qml.expval(qml.PauliZ(1)) res = circuit(a, b, c) out_state = np.kron(Rotx(c.numpy()), I) @ np.kron(Roty(b.numpy()), I) @ CNOT \ @ np.kron(Rotz(b.numpy()), I) @ np.kron(Rotx(a.numpy()), I) @ np.array([1, 0, 0, 0]) ex0 = np.vdot(out_state, np.kron(Y, I) @ out_state) ex1 = np.vdot(out_state, np.kron(I, Z) @ out_state) ex = np.array([ex0, ex1]) self.assertAllAlmostEqual(ex, res.numpy(), delta=self.tol)