コード例 #1
0
ファイル: test_operation.py プロジェクト: stephanbm/pennylane
    def test_crx_decomposition_correctness(self, phi, tol):
        """Test that the decomposition of the controlled X
        qubit rotation is correct"""

        expected = CRotx(phi)

        obtained = np.kron(I, Rotz(-np.pi / 2)) @ CNOT @ np.kron(
            I, Roty(-phi / 2)) @ CNOT @ np.kron(I, Roty(phi / 2)) @ np.kron(
                I, Rotz(np.pi / 2))
        assert np.allclose(expected, obtained, atol=tol, rtol=0)
コード例 #2
0
    def test_pauliz_tensor_hadamard(self, theta, phi, varphi, tol_stochastic):
        """Test that a tensor product involving PauliZ and hadamard works correctly"""
        dev = qml.device("default.qubit", wires=3, shots=int(1e6))

        @qml.qnode(dev, diff_method="parameter-shift")
        def circuit(a, b, c):
            ansatz(a, b, c)
            return sample(qml.PauliZ(0) @ qml.Hadamard(1) @ qml.PauliY(2))

        s1 = circuit(theta, phi, varphi)

        zero_state = np.zeros(2**3)
        zero_state[0] = 1
        psi = zero_state
        psi = tensor_product([Rotx(theta), I, I]) @ zero_state
        psi = tensor_product([I, Rotx(phi), I]) @ psi
        psi = tensor_product([I, I, Rotx(varphi)]) @ psi
        psi = tensor_product([CNOT, I]) @ psi
        psi = tensor_product([I, CNOT]) @ psi

        # Diagonalize according to the observable
        psi = tensor_product([I, Roty(-np.pi / 4), I]) @ psi
        psi = tensor_product([I, I, Z]) @ psi
        psi = tensor_product([I, I, S]) @ psi
        psi = tensor_product([I, I, H]) @ psi

        expected_probabilities = np.abs(psi)**2

        assert np.allclose(dev.probability(),
                           expected_probabilities,
                           atol=tol_stochastic,
                           rtol=0)

        # s1 should only contain 1 and -1
        assert np.allclose(s1**2, 1, atol=tol_stochastic, rtol=0)
コード例 #3
0
ファイル: test_torch.py プロジェクト: zaqqwerty/pennylane
    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)