How to use the pennylane.Hadamard function in PennyLane

To help you get started, we’ve selected a few PennyLane examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github XanaduAI / pennylane-cirq / tests / test_sample.py View on Github external
def test_pauliz_hadamard(self, device, shots, tol):
        """Test that a tensor product involving PauliZ and PauliY and hadamard works correctly"""
        theta = 0.432
        phi = 0.123
        varphi = -0.543

        dev = device(3)

        obs = (
            qml.PauliZ(wires=[0], do_queue=False)
            @ qml.Hadamard(wires=[1], do_queue=False)
            @ qml.PauliY(wires=[2], do_queue=False)
        )

        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]),
                ],
                rotations=obs.diagonalizing_gates(),
            )

        s1 = dev.sample(obs)
github XanaduAI / pennylane-cirq / tests / test_cirq_device.py View on Github external
            (qml.Hadamard(wires=[0]).inv(), [cirq.H ** -1]),
            (qml.S(wires=[0]), [cirq.S]),
            (qml.S(wires=[0]).inv(), [cirq.S ** -1]),
            (qml.PhaseShift(1.4, wires=[0]), [cirq.ZPowGate(exponent=1.4 / np.pi)]),
            (qml.PhaseShift(-1.2, wires=[0]), [cirq.ZPowGate(exponent=-1.2 / np.pi)]),
            (qml.PhaseShift(2, wires=[0]), [cirq.ZPowGate(exponent=2 / np.pi)]),
            (
                qml.PhaseShift(1.4, wires=[0]).inv(),
                [cirq.ZPowGate(exponent=-1.4 / np.pi)],
            ),
            (
                qml.PhaseShift(-1.2, wires=[0]).inv(),
                [cirq.ZPowGate(exponent=1.2 / np.pi)],
            ),
            (qml.PhaseShift(2, wires=[0]).inv(), [cirq.ZPowGate(exponent=-2 / np.pi)]),
            (qml.RX(1.4, wires=[0]), [cirq.rx(1.4)]),
            (qml.RX(-1.2, wires=[0]), [cirq.rx(-1.2)]),
github rigetti / pennylane-forest / tests / test_qvm.py View on Github external
def test_hadamard_expectation(self, shots, qvm, compiler):
        """Test that Hadamard expectation value is correct"""
        theta = 0.432
        phi = 0.123

        dev = plf.QVMDevice(device="2q-qvm", shots=shots)
        dev.apply("RY", wires=[0], par=[theta])
        dev.apply("RY", wires=[1], par=[phi])
        dev.apply("CNOT", wires=[0, 1], par=[])

        O = qml.Hadamard
        name = "Hadamard"

        dev._obs_queue = [O(wires=[0], do_queue=False), O(wires=[1], do_queue=False)]
        dev.pre_measure()

        res = np.array([dev.expval(name, [0], []), dev.expval(name, [1], [])])
        # below are the analytic expectation values for this circuit
        expected = np.array(
            [np.sin(theta) * np.sin(phi) + np.cos(theta), np.cos(theta) * np.cos(phi) + np.sin(phi)]
        ) / np.sqrt(2)
        self.assertAllAlmostEqual(res, expected, delta=3 / np.sqrt(shots))
github XanaduAI / pennylane / tests / test_operation.py View on Github external
def test_multiply_tensor_in_place(self):
        """Test that multiplying a tensor in-place
        produces a tensor"""
        X = qml.PauliX(0)
        Y = qml.PauliY(2)
        Z = qml.PauliZ(1)
        H = qml.Hadamard(3)

        t = X
        t @= Y
        t @= Z @ H

        assert isinstance(t, Tensor)
        assert t.obs == [X, Y, Z, H]
github XanaduAI / pennylane-cirq / tests / test_apply.py View on Github external
def test_basis_state_not_first_operation(self, device):
        """Test that an exception is raised if BasisState is
        applied to a circuit not in the ground state"""
        dev = device(4)
        state = np.array([0, 0, 1, 0])

        with mimic_execution_for_apply(dev):
            dev.apply([qml.Hadamard(wires=[0])])

            with pytest.raises(
                qml.DeviceError, match="is only supported at the beginning of a circuit"
            ):
                dev.apply([qml.BasisState(state, wires=[0, 1, 2, 3])])
github XanaduAI / pennylane-cirq / tests / test_simulator_device.py View on Github external
            (qml.Hadamard, [1 / math.sqrt(2), -1 / math.sqrt(2)], [0, 1]),
        ],
    )
    def test_apply_operation_single_wire_no_parameters(
        self, simulator_device_1_wire, tol, op, input, expected_output
    ):
        """Tests that applying an operation yields the expected output state for single wire
           operations that have no parameters."""

        simulator_device_1_wire.reset()
        simulator_device_1_wire._initial_state = np.array(input, dtype=np.complex64)
        simulator_device_1_wire.apply([op(wires=[0])])

        assert np.allclose(
            simulator_device_1_wire.state, np.array(expected_output), **tol
        )
github XanaduAI / pennylane / tests / test_vqe.py View on Github external
def custom_fixed_ansatz(*params, wires=None):
    """Custom fixed ansatz"""
    qml.RX(0.5, wires=0)
    qml.RX(-1.2, wires=1)
    qml.Hadamard(wires=0)
    qml.CNOT(wires=[0, 1])
    qml.Hadamard(wires=1)
    qml.CNOT(wires=[0, 1])
github XanaduAI / qml / demonstrations / tutorial_noisy_circuit_optimization.py View on Github external
def bell_pair():
        qml.Hadamard(wires=0)
        qml.CNOT(wires=[0, 1])
        cirq_ops.BitFlip(p, wires=0)
        cirq_ops.BitFlip(p, wires=1)
    # measuring the circuits will now use the new noisy bell_pair() function    
github XanaduAI / qml / demonstrations / tutorial_QGAN.py View on Github external
def discriminator(w):
    qml.Hadamard(wires=0)
    qml.RX(w[0], wires=0)
    qml.RX(w[1], wires=2)
    qml.RY(w[2], wires=0)
    qml.RY(w[3], wires=2)
    qml.RZ(w[4], wires=0)
    qml.RZ(w[5], wires=2)
    qml.CNOT(wires=[0, 2])
    qml.RX(w[6], wires=2)
    qml.RY(w[7], wires=2)
    qml.RZ(w[8], wires=2)
github rigetti / pennylane-forest / pennylane_forest / converter.py View on Github external
import warnings
from collections.abc import Sequence

import numpy as np
import pennylane as qml
import pennylane_forest as plf
import pyquil
from pyquil.gates import Gate

pyquil_inv_operation_map = {
    "I": qml.Identity,
    "X": qml.PauliX,
    "Y": qml.PauliY,
    "Z": qml.PauliZ,
    "H": qml.Hadamard,
    "CNOT": qml.CNOT,
    "SWAP": qml.SWAP,
    "CZ": qml.CZ,
    "PHASE": qml.PhaseShift,
    "RX": qml.RX,
    "RY": qml.RY,
    "RZ": qml.RZ,
    "CRX": qml.CRX,
    "CRY": qml.CRY,
    "CRZ": qml.CRZ,
    "S": plf.ops.S,
    "T": plf.ops.T,
    "CCNOT": plf.ops.CCNOT,
    "CPHASE": lambda *params, wires: plf.ops.CPHASE(*params, 3, wires=wires),
    "CPHASE00": lambda *params, wires: plf.ops.CPHASE(*params, 0, wires=wires),
    "CPHASE01": lambda *params, wires: plf.ops.CPHASE(*params, 1, wires=wires),

PennyLane

PennyLane is a cross-platform Python library for quantum computing, quantum machine learning, and quantum chemistry. Train a quantum computer the same way as a neural network.

Apache-2.0
Latest version published 19 days ago

Package Health Score

87 / 100
Full package analysis