def test_sample_values_projector(self, device, shots, tol):
        """Tests if the samples of a Projector observable returned by sample have
        the correct values
        """
        dev = device(1)

        theta = 0.543

        with mimic_execution_for_sample(dev):
            dev.apply([qml.RX(theta, wires=[0])])

        dev._obs_queue = [qml.Projector([0], wires=0, do_queue=False)]
        dev._obs_queue[0].return_type = qml.operation.Sample
        s1 = dev.sample(qml.Projector([0], wires=[0]))
        # s1 should only contain 0 or 1, the eigenvalues of the projector
        assert np.allclose(sorted(list(set(s1))), [0, 1], **tol)
        assert np.allclose(np.mean(s1), np.cos(theta / 2) ** 2, **tol)
        assert np.allclose(
            np.var(s1), np.cos(theta / 2) ** 2 - (np.cos(theta / 2) ** 2) ** 2, **tol
        )

        dev._obs_queue = [qml.Projector([1], wires=0, do_queue=False)]
        dev._obs_queue[0].return_type = qml.operation.Sample
        s1 = dev.sample(qml.Projector([1], wires=[0]))
        assert np.allclose(sorted(list(set(s1))), [0, 1], **tol)
        assert np.allclose(np.mean(s1), np.sin(theta / 2) ** 2, **tol)
        assert np.allclose(
            np.var(s1), np.sin(theta / 2) ** 2 - (np.sin(theta / 2) ** 2) ** 2, **tol
        )
Example #2
0
def test_projection():
    """Test that the Projector observable is correctly supported."""
    wires = 2
    dev = BraketLocalQubitDevice(wires=wires)

    thetas = [1.5, 1.6]
    p_01 = np.cos(thetas[0] / 2)**2 * np.sin(thetas[1] / 2)**2
    p_10 = np.sin(thetas[0] / 2)**2 * np.cos(thetas[1] / 2)**2

    def f(thetas, **kwargs):
        [qml.RY(thetas[i], wires=i) for i in range(wires)]

    measure_types = ["expval", "var", "sample"]
    projector_01 = qml.Projector([0, 1], wires=range(wires))
    projector_10 = qml.Projector([1, 0], wires=range(wires))

    # 01 case
    fs = [qml.map(f, [projector_01], dev, measure=m) for m in measure_types]
    assert np.allclose(fs[0](thetas), p_01)
    assert np.allclose(fs[1](thetas), p_01 - p_01**2)

    samples = fs[2](thetas, shots=100)[0].tolist()
    assert set(samples) == {0, 1}

    # 10 case
    fs = [qml.map(f, [projector_10], dev, measure=m) for m in measure_types]
    assert np.allclose(fs[0](thetas), p_10)
    assert np.allclose(fs[1](thetas), p_10 - p_10**2)

    samples = fs[2](thetas, shots=100)[0].tolist()
    assert set(samples) == {0, 1}
Example #3
0
    def test_projector(self, device, shots, tol):
        """Test that a tensor product involving qml.Projector works correctly"""
        theta = 0.432
        phi = 0.123
        varphi = -0.543

        dev = device(3)

        with mimic_execution_for_var(dev):
            dev.apply([
                qml.RX(theta, wires=[0]),
                qml.RX(phi, wires=[1]),
                qml.RX(varphi, wires=[2]),
                qml.CNOT(wires=[0, 1]),
                qml.CNOT(wires=[1, 2]),
            ])

        obs = qml.PauliZ(wires=[0], do_queue=False) @ qml.Projector(
            [0, 0], wires=[1, 2], do_queue=False)
        res = dev.var(obs)
        expected = (
            (np.cos(varphi / 2) * np.cos(phi / 2) * np.cos(theta / 2))**2 +
            (np.cos(varphi / 2) * np.sin(phi / 2) * np.sin(theta / 2))**2
        ) - ((np.cos(varphi / 2) * np.cos(phi / 2) * np.cos(theta / 2))**2 -
             (np.cos(varphi / 2) * np.sin(phi / 2) * np.sin(theta / 2))**2)**2
        assert np.allclose(res, expected, **tol)

        obs = qml.PauliZ(wires=[0], do_queue=False) @ qml.Projector(
            [0, 1], wires=[1, 2], do_queue=False)
        res = dev.var(obs)
        expected = (
            (np.sin(varphi / 2) * np.cos(phi / 2) * np.cos(theta / 2))**2 +
            (np.sin(varphi / 2) * np.sin(phi / 2) * np.sin(theta / 2))**2
        ) - ((np.sin(varphi / 2) * np.cos(phi / 2) * np.cos(theta / 2))**2 -
             (np.sin(varphi / 2) * np.sin(phi / 2) * np.sin(theta / 2))**2)**2
        assert np.allclose(res, expected, **tol)

        obs = qml.PauliZ(wires=[0], do_queue=False) @ qml.Projector(
            [1, 0], wires=[1, 2], do_queue=False)
        res = dev.var(obs)
        expected = (
            (np.sin(varphi / 2) * np.sin(phi / 2) * np.cos(theta / 2))**2 +
            (np.sin(varphi / 2) * np.cos(phi / 2) * np.sin(theta / 2))**2
        ) - ((np.sin(varphi / 2) * np.sin(phi / 2) * np.cos(theta / 2))**2 -
             (np.sin(varphi / 2) * np.cos(phi / 2) * np.sin(theta / 2))**2)**2
        assert np.allclose(res, expected, **tol)

        obs = qml.PauliZ(wires=[0], do_queue=False) @ qml.Projector(
            [1, 1], wires=[1, 2], do_queue=False)
        res = dev.var(obs)
        expected = (
            (np.cos(varphi / 2) * np.sin(phi / 2) * np.cos(theta / 2))**2 +
            (np.cos(varphi / 2) * np.cos(phi / 2) * np.sin(theta / 2))**2
        ) - ((np.cos(varphi / 2) * np.sin(phi / 2) * np.cos(theta / 2))**2 -
             (np.cos(varphi / 2) * np.cos(phi / 2) * np.sin(theta / 2))**2)**2
        assert np.allclose(res, expected, **tol)
Example #4
0
 def circuit(basis_state):
     qml.RX(theta, wires=[0])
     qml.RX(phi, wires=[1])
     qml.RX(varphi, wires=[2])
     qml.CNOT(wires=[0, 1])
     qml.CNOT(wires=[1, 2])
     return qml.sample(qml.PauliZ(wires=[0]) @ qml.Projector(basis_state, wires=[1, 2]))
Example #5
0
    def test_projector_eigvals(self, basis_state, tol):
        """Tests that the eigvals property of the Projector class returns the correct results."""
        num_wires = len(basis_state)
        eigvals = qml.Projector(basis_state, wires=range(num_wires)).eigvals

        if basis_state[0] == 0:
            observable = np.array([[1, 0], [0, 0]])
        elif basis_state[0] == 1:
            observable = np.array([[0, 0], [0, 1]])
        for i in basis_state[1:]:
            if i == 0:
                observable = np.kron(observable, np.array([[1, 0], [0, 0]]))
            elif i == 1:
                observable = np.kron(observable, np.array([[0, 0], [0, 1]]))
        expected_eigvals, expected_eigvecs = np.linalg.eig(observable)

        assert np.allclose(np.sort(eigvals),
                           np.sort(expected_eigvals),
                           atol=tol,
                           rtol=0)
        assert np.allclose(
            eigvals,
            expected_eigvecs[np.where(expected_eigvals == 1)[0][0]],
            atol=tol,
            rtol=0)
Example #6
0
    def test_projector_diagonalization(self, basis_state, tol):
        """Test that the diagonalizing_gates property of the Projector class returns empty."""
        num_wires = len(basis_state)
        diag_gates = qml.Projector(
            basis_state, wires=range(num_wires)).diagonalizing_gates()

        assert diag_gates == []
Example #7
0
    def test_projector_variance(self, tol):
        """Test that the variance of a projector is correctly returned"""
        dev = qml.device("default.qubit", wires=2)
        P = np.array([1])
        x, y = 0.765, -0.654

        with qml.tape.JacobianTape() as tape:
            qml.RX(x, wires=0)
            qml.RY(y, wires=1)
            qml.CNOT(wires=[0, 1])
            qml.var(qml.Projector(P, wires=0) @ qml.PauliX(1))

        tape.trainable_params = {0, 1}

        res = tape.execute(dev)
        expected = 0.25 * np.sin(
            x / 2)**2 * (3 + np.cos(2 * y) + 2 * np.cos(x) * np.sin(y)**2)
        assert np.allclose(res, expected, atol=tol, rtol=0)

        # # circuit jacobians
        tapes, fn = qml.gradients.param_shift(tape)
        gradA = fn(dev.batch_execute(tapes))

        tapes, fn = qml.gradients.finite_diff(tape)
        gradF = fn(dev.batch_execute(tapes))

        expected = np.array([[
            0.5 * np.sin(x) *
            (np.cos(x / 2)**2 + np.cos(2 * y) * np.sin(x / 2)**2),
            -2 * np.cos(y) * np.sin(x / 2)**4 * np.sin(y),
        ]])
        assert gradA == pytest.approx(expected, abs=tol)
        assert gradF == pytest.approx(expected, abs=tol)
 def circuit(theta, phi, varphi):
     qml.RX(theta, wires=[0])
     qml.RX(phi, wires=[1])
     qml.RX(varphi, wires=[2])
     qml.CNOT(wires=[0, 1])
     qml.CNOT(wires=[1, 2])
     return qml.expval(
         qml.PauliZ(wires=[0]) @ qml.Projector([1, 1], wires=[1, 2]))
Example #9
0
    def test_proj_unsupported(self, dev):
        """Test if a QuantumFunctionError is raised for a Projector observable"""
        with qml.tape.QuantumTape() as tape:
            qml.CRX(0.1, wires=[0, 1])
            qml.expval(qml.Projector([0, 1], wires=[0, 1]))

        with pytest.raises(
                qml.QuantumFunctionError,
                match="differentiation method does not support the Projector"):
            dev.adjoint_jacobian(tape)

        with qml.tape.QuantumTape() as tape:
            qml.CRX(0.1, wires=[0, 1])
            qml.expval(qml.Projector([0], wires=[0]) @ qml.PauliZ(0))

        with pytest.raises(
                qml.QuantumFunctionError,
                match="differentiation method does not support the Projector"):
            dev.adjoint_jacobian(tape)
Example #10
0
    def test_var_projector(self, device, shots, tol):
        """Tests for variance calculation using an arbitrary Projector observable"""
        dev = device(2)

        phi = 0.543
        theta = 0.654

        with mimic_execution_for_var(dev):
            dev.apply([
                qml.RX(phi, wires=[0]),
                qml.RY(theta, wires=[1]),
                qml.CNOT(wires=[0, 1])
            ])

        obs = qml.Projector([0, 0], wires=[0, 1], do_queue=False)
        var = dev.var(obs)
        expected = (np.cos(phi / 2) * np.cos(theta / 2))**2 - (
            (np.cos(phi / 2) * np.cos(theta / 2))**2)**2
        assert np.allclose(var, expected, **tol)

        obs = qml.Projector([0, 1], wires=[0, 1], do_queue=False)
        var = dev.var(obs)
        expected = (np.cos(phi / 2) * np.sin(theta / 2))**2 - (
            (np.cos(phi / 2) * np.sin(theta / 2))**2)**2
        assert np.allclose(var, expected, **tol)

        obs = qml.Projector([1, 0], wires=[0, 1], do_queue=False)
        var = dev.var(obs)
        expected = (np.sin(phi / 2) * np.sin(theta / 2))**2 - (
            (np.sin(phi / 2) * np.sin(theta / 2))**2)**2
        assert np.allclose(var, expected, **tol)

        obs = qml.Projector([1, 1], wires=[0, 1], do_queue=False)
        var = dev.var(obs)
        expected = (np.sin(phi / 2) * np.cos(theta / 2))**2 - (
            (np.sin(phi / 2) * np.cos(theta / 2))**2)**2
        assert np.allclose(var, expected, **tol)
Example #11
0
    def test_sample_values_projector_multi_qubit(self, device, shots, tol):
        """Tests if the samples of a multi-qubit Projector observable returned by sample have
        the correct values
        """
        dev = device(2)
        theta = 0.543

        with mimic_execution_for_sample(dev):
            dev.apply(
                [
                    qml.RX(theta, wires=[0]),
                    qml.RY(2 * theta, wires=[1]),
                    qml.CNOT(wires=[0, 1]),
                ]
            )

        dev._obs_queue = [qml.Projector([0, 0], wires=[0, 1], do_queue=False)]
        dev._obs_queue[0].return_type = qml.operation.Sample
        s1 = dev.sample(qml.Projector([0, 0], wires=[0, 1]))
        # s1 should only contain 0 or 1, the eigenvalues of the projector
        assert np.allclose(sorted(list(set(s1))), [0, 1], **tol)
        expected = (np.cos(theta / 2) * np.cos(theta)) ** 2
        assert np.allclose(np.mean(s1), expected, **tol)

        dev._obs_queue = [qml.Projector([0, 1], wires=[0, 1], do_queue=False)]
        dev._obs_queue[0].return_type = qml.operation.Sample
        s1 = dev.sample(qml.Projector([0, 1], wires=[0, 1]))
        assert np.allclose(sorted(list(set(s1))), [0, 1], **tol)
        expected = (np.cos(theta / 2) * np.sin(theta)) ** 2
        assert np.allclose(np.mean(s1), expected, **tol)

        dev._obs_queue = [qml.Projector([1, 0], wires=[0, 1], do_queue=False)]
        dev._obs_queue[0].return_type = qml.operation.Sample
        s1 = dev.sample(qml.Projector([1, 0], wires=[0, 1]))
        assert np.allclose(sorted(list(set(s1))), [0, 1], **tol)
        expected = (np.sin(theta / 2) * np.sin(theta)) ** 2
        assert np.allclose(np.mean(s1), expected, **tol)

        dev._obs_queue = [qml.Projector([1, 1], wires=[0, 1], do_queue=False)]
        dev._obs_queue[0].return_type = qml.operation.Sample
        s1 = dev.sample(qml.Projector([1, 1], wires=[0, 1]))
        assert np.allclose(sorted(list(set(s1))), [0, 1], **tol)
        expected = (np.sin(theta / 2) * np.cos(theta)) ** 2
        assert np.allclose(np.mean(s1), expected, **tol)
Example #12
0
def kernel(x1, x2, params):
    ansatz(x1, params, wires)
    qml.adjoint(ansatz)(x2, params, wires)
    return qml.expval(qml.Projector([0] * width, wires=wires))
Example #13
0
 def generator(self):
     return qml.Projector(np.array([0, 0]), wires=self.wires)
Example #14
0
 def circuit(basis_state):
     obs = qml.Projector(basis_state, wires=range(2))
     return qml.expval(obs)
 def test_tensor_product_of_valid_terms(self):
     """Tests if return is true for a tensor product of Pauli, Hadamard, and Projector terms"""
     o = qml.PauliZ(0) @ qml.Hadamard(1) @ qml.Projector([0], wires=2)
     assert _obs_has_kernel(o)
Example #16
0
 def circuit(basis_state):
     qml.RX(theta, wires=[0])
     return qml.sample(qml.Projector(basis_state, wires=0))
Example #17
0
# observables for which device support is tested
obs = {
    "Identity":
    qml.Identity(wires=[0]),
    "Hadamard":
    qml.Hadamard(wires=[0]),
    "Hermitian":
    qml.Hermitian(np.eye(2), wires=[0]),
    "PauliX":
    qml.PauliX(wires=[0]),
    "PauliY":
    qml.PauliY(wires=[0]),
    "PauliZ":
    qml.PauliZ(wires=[0]),
    "Projector":
    qml.Projector(np.array([1]), wires=[0]),
    "SparseHamiltonian":
    qml.SparseHamiltonian(coo_matrix(np.eye(8)), wires=[0, 1, 2]),
    "Hamiltonian":
    qml.Hamiltonian([1, 1], [qml.PauliZ(0), qml.PauliX(0)]),
}

all_obs = obs.keys()

# All qubit observables should be available to test in the device test suite
all_available_obs = qml.ops._qubit__obs__.copy()  # pylint: disable=protected-access
# Note that the identity is not technically a qubit observable
all_available_obs |= {"Identity"}

if not set(all_obs) == all_available_obs:
    raise ValueError(
Example #18
0
 def circuit(phi, theta):
     qml.RY(theta, wires=[0])
     qml.RY(phi, wires=[1])
     qml.CNOT(wires=[0, 1])
     return qml.expval(qml.Projector([1, 1], wires=[0, 1]))
 def circuit(x, y):
     qml.RX(x, wires=0)
     qml.RY(y, wires=1)
     qml.CNOT(wires=[0, 1])
     return qml.var(qml.Projector(P, wires=0) @ qml.PauliX(1))
Example #20
0
    def test_projector(self, device, shots, tol):  # WIP
        """Test that a tensor product involving qml.Projector works correctly"""
        theta = 0.832
        phi = 0.123
        varphi = -0.543

        # Reseed here so that all eigenvalues are guaranteed to appear in the sample for all projected basis states
        np.random.seed(8)

        dev = device(3)

        with mimic_execution_for_sample(dev):
            dev.apply(
                [
                    qml.RX(theta, wires=[0]),
                    qml.RX(phi, wires=[1]),
                    qml.RX(varphi, wires=[2]),
                    qml.CNOT(wires=[0, 1]),
                    qml.CNOT(wires=[1, 2]),
                ]
            )

        obs = qml.PauliZ(wires=[0], do_queue=False) @ qml.Projector(
            [0, 0], wires=[1, 2], do_queue=False
        )
        s1 = dev.sample(obs)
        # s1 should only contain the eigenvalues of the projector matrix tensor product Z, i.e. {-1, 0, 1}
        assert np.allclose(sorted(list(set(s1))), [-1, 0, 1], **tol)
        mean = np.mean(s1)
        expected = (np.cos(varphi / 2) * np.cos(phi / 2) * np.cos(theta / 2)) ** 2 - (
            np.cos(varphi / 2) * np.sin(phi / 2) * np.sin(theta / 2)
        ) ** 2
        assert np.allclose(mean, expected, **tol)
        var = np.var(s1)
        expected = (
            (np.cos(varphi / 2) * np.cos(phi / 2) * np.cos(theta / 2)) ** 2
            + (np.cos(varphi / 2) * np.sin(phi / 2) * np.sin(theta / 2)) ** 2
            - (
                (np.cos(varphi / 2) * np.cos(phi / 2) * np.cos(theta / 2)) ** 2
                - (np.cos(varphi / 2) * np.sin(phi / 2) * np.sin(theta / 2)) ** 2
            )
            ** 2
        )
        assert np.allclose(var, expected, **tol)

        obs = qml.PauliZ(wires=[0], do_queue=False) @ qml.Projector(
            [0, 1], wires=[1, 2], do_queue=False
        )
        s1 = dev.sample(obs)
        assert np.allclose(sorted(list(set(s1))), [-1, 0, 1], **tol)
        mean = np.mean(s1)
        expected = (np.sin(varphi / 2) * np.cos(phi / 2) * np.cos(theta / 2)) ** 2 - (
            np.sin(varphi / 2) * np.sin(phi / 2) * np.sin(theta / 2)
        ) ** 2
        assert np.allclose(mean, expected, **tol)
        var = np.var(s1)
        expected = (
            (np.sin(varphi / 2) * np.cos(phi / 2) * np.cos(theta / 2)) ** 2
            + (np.sin(varphi / 2) * np.sin(phi / 2) * np.sin(theta / 2)) ** 2
            - (
                (np.sin(varphi / 2) * np.cos(phi / 2) * np.cos(theta / 2)) ** 2
                - (np.sin(varphi / 2) * np.sin(phi / 2) * np.sin(theta / 2)) ** 2
            )
            ** 2
        )
        assert np.allclose(var, expected, **tol)

        obs = qml.PauliZ(wires=[0], do_queue=False) @ qml.Projector(
            [1, 0], wires=[1, 2], do_queue=False
        )
        s1 = dev.sample(obs)
        assert np.allclose(sorted(list(set(s1))), [-1, 0, 1], **tol)
        mean = np.mean(s1)
        expected = (np.sin(varphi / 2) * np.sin(phi / 2) * np.cos(theta / 2)) ** 2 - (
            np.sin(varphi / 2) * np.cos(phi / 2) * np.sin(theta / 2)
        ) ** 2
        assert np.allclose(mean, expected, **tol)
        var = np.var(s1)
        expected = (
            (np.sin(varphi / 2) * np.sin(phi / 2) * np.cos(theta / 2)) ** 2
            + (np.sin(varphi / 2) * np.cos(phi / 2) * np.sin(theta / 2)) ** 2
            - (
                (np.sin(varphi / 2) * np.sin(phi / 2) * np.cos(theta / 2)) ** 2
                - (np.sin(varphi / 2) * np.cos(phi / 2) * np.sin(theta / 2)) ** 2
            )
            ** 2
        )
        assert np.allclose(var, expected, **tol)

        obs = qml.PauliZ(wires=[0], do_queue=False) @ qml.Projector(
            [1, 1], wires=[1, 2], do_queue=False
        )
        s1 = dev.sample(obs)
        assert np.allclose(sorted(list(set(s1))), [-1, 0, 1], **tol)
        mean = np.mean(s1)
        expected = (np.cos(varphi / 2) * np.sin(phi / 2) * np.cos(theta / 2)) ** 2 - (
            np.cos(varphi / 2) * np.cos(phi / 2) * np.sin(theta / 2)
        ) ** 2
        assert np.allclose(mean, expected, **tol)
        var = np.var(s1)
        expected = (
            (np.cos(varphi / 2) * np.sin(phi / 2) * np.cos(theta / 2)) ** 2
            + (np.cos(varphi / 2) * np.cos(phi / 2) * np.sin(theta / 2)) ** 2
            - (
                (np.cos(varphi / 2) * np.sin(phi / 2) * np.cos(theta / 2)) ** 2
                - (np.cos(varphi / 2) * np.cos(phi / 2) * np.sin(theta / 2)) ** 2
            )
            ** 2
        )
        assert np.allclose(var, expected, **tol)
Example #21
0
 def circuit(basis_state):
     qml.RY(theta, wires=[0])
     qml.RY(phi, wires=[1])
     qml.CNOT(wires=[0, 1])
     return qml.expval(qml.Projector(basis_state, wires=[0, 1]))
Example #22
0
        with pytest.raises(ValueError, match="Basis state must be of length"):
            basis_state = np.random.randint(2, size=(3))
            circuit(basis_state)

        with pytest.raises(ValueError,
                           match="Basis state must only consist of 0s"):
            basis_state = np.array([0, 2])
            circuit(basis_state)


def test_identity_eigvals(tol):
    """Test identity eigenvalues are correct"""
    res = qml.Identity._eigvals()
    expected = np.array([1, 1])
    assert np.allclose(res, expected, atol=tol, rtol=0)


label_data = [
    (qml.Hermitian(np.eye(2), wires=1), "𝓗"),
    (qml.Identity(wires=0), "I"),
    (qml.Projector([1, 0, 1], wires=(0, 1, 2)), "|101⟩⟨101|"),
]


@pytest.mark.parametrize("op, label", label_data)
def test_label_method(op, label):
    assert op.label() == label
    assert op.label(decimals=5) == label
    assert op.label(base_label="obs") == "obs"
Example #23
0
 def circuit(basis_state):
     qml.RX(theta, wires=[0])
     qml.RY(2 * theta, wires=[1])
     qml.CNOT(wires=[0, 1])
     return qml.sample(qml.Projector(basis_state, wires=[0, 1]))
 def test_projector(self):
     """Tests if return is true for a Projector observable"""
     o = qml.Projector([0], wires=0)
     assert _obs_has_kernel(o)