Exemple #1
0
def basic_variational_circuit(params, rotations, masked_circuit: MaskedCircuit):
    full_parameters = masked_circuit.expanded_parameters(params)
    wires = masked_circuit.wires
    dropout_mask = masked_circuit.full_mask(DropoutMask)
    for wire, _is_masked in enumerate(masked_circuit.mask(Axis.WIRES)):
        qml.RY(np.pi / 4, wires=wire)
    r = -1
    for layer, _is_layer_masked in enumerate(masked_circuit.mask(Axis.LAYERS)):
        for wire, _is_wire_masked in enumerate(masked_circuit.mask(Axis.WIRES)):
            r += 1
            if dropout_mask[layer][wire]:
                continue
            if rotations[r] == 0:
                rotation = qml.RX
            elif rotations[r] == 1:
                rotation = qml.RY
            else:
                rotation = qml.RZ
            rotation(full_parameters[layer][wire], wires=wire)

        for wire in range(0, wires - 1, 2):
            if (
                Axis.ENTANGLING in masked_circuit.masks
                and masked_circuit.mask(Axis.ENTANGLING)[layer, wire]
            ):
                continue
            qml.CZ(wires=[wire, wire + 1])
        for wire in range(1, wires - 1, 2):
            if (
                Axis.ENTANGLING in masked_circuit.masks
                and masked_circuit.mask(Axis.ENTANGLING)[layer, wire]
            ):
                continue
            qml.CZ(wires=[wire, wire + 1])
Exemple #2
0
def variational_ansatz_zero(params, wires):

    n_qubits = len(wires)
    D1 = [0,1]
    D2 = [i for i in range(len(D1) ,params.shape[0]-1)]
    depth = params.shape[0]
    #n_rotations = len(D1)

    qml.BasisState(np.array([0 for i in range(n_qubits)]), wires=[i for i in range(n_qubits)])
    for d in D1:
        for i in range(n_qubits//2, n_qubits):

            qml.RY(params[d,i],wires=[i]) 
            qml.RZ(params[d,i],wires=[i])
    for d2 in D2:
        for i in range(n_qubits):
            qml.RY(params[d2,i],wires=[i]) 
            qml.RZ(params[d2,i],wires=[i])             

        for i in range(n_qubits//2):

            qml.CZ(wires=[2*i, 2*i + 1])   #circuit.add_gate(CZ(2*i, 2*i+1))
        for i in range(n_qubits//2 - 1):
            qml.CZ(wires=[2*i+1, 2*i + 2])   #circuit.add_gate(CZ(2*i+1, 2*i+2))
    for i in range(n_qubits):
        qml.RY(params[depth-1,i],wires=[i]) 
        qml.RZ(params[depth-1,i],wires=[i])
Exemple #3
0
    def expand(self):

        with qml.tape.QuantumTape() as tape:

            # initial rotations
            for i in range(len(self.wires)):
                qml.RY(self.parameters[0][i], wires=self.wires[i])

            for layer in range(self.n_layers):

                # even layer of entanglers
                even_wires = [
                    self.wires[i:i + 2] for i in range(0,
                                                       len(self.wires) - 1, 2)
                ]
                for i, wire_pair in enumerate(even_wires):
                    qml.CZ(wires=wire_pair)
                    qml.RY(self.parameters[1][layer, i, 0], wires=wire_pair[0])
                    qml.RY(self.parameters[1][layer, i, 1], wires=wire_pair[1])

                # odd layer of entanglers
                odd_wires = [
                    self.wires[i:i + 2] for i in range(1,
                                                       len(self.wires) - 1, 2)
                ]
                for i, wire_pair in enumerate(odd_wires):
                    qml.CZ(wires=wire_pair)
                    qml.RY(self.parameters[1][layer,
                                              len(self.wires) // 2 + i, 0],
                           wires=wire_pair[0])
                    qml.RY(self.parameters[1][layer,
                                              len(self.wires) // 2 + i, 1],
                           wires=wire_pair[1])

        return tape
    def qfunc(a, b, c, angles):
        qml.RX(a, wires=0)
        qml.RX(b, wires=1)
        qml.PauliZ(1)
        qml.CNOT(wires=[0, 1]).inv()
        qml.CRY(b, wires=[3, 1])
        qml.RX(angles[0], wires=0)
        qml.RX(4 * angles[1], wires=1)
        qml.PhaseShift(17 / 9 * c, wires=2)
        qml.RZ(b, wires=3)
        qml.RX(angles[2], wires=2).inv()
        qml.CRY(0.3589, wires=[3, 1]).inv()
        qml.CSWAP(wires=[4, 2, 1]).inv()
        qml.QubitUnitary(np.eye(2), wires=[2])
        qml.Toffoli(wires=[0, 2, 1])
        qml.CNOT(wires=[0, 2])
        qml.PauliZ(wires=[1])
        qml.PauliZ(wires=[1]).inv()
        qml.CZ(wires=[0, 1])
        qml.CZ(wires=[0, 2]).inv()
        qml.CNOT(wires=[2, 1])
        qml.CNOT(wires=[0, 2])
        qml.SWAP(wires=[0, 2]).inv()
        qml.CNOT(wires=[1, 3])
        qml.RZ(b, wires=3)
        qml.CSWAP(wires=[4, 0, 1])

        return [
            qml.expval(qml.PauliY(0)),
            qml.var(qml.Hadamard(wires=1)),
            qml.sample(qml.PauliX(2)),
            qml.expval(qml.Hermitian(np.eye(4), wires=[3, 4])),
        ]
Exemple #5
0
 def entangler(self, layer):
     if layer%2==0:
         for wire in range(0, self.width//2, 1):
             qml.CZ(wires=[2*wire, 2*wire + 1])
     if layer%2==1:
         for wire in range(1, self.width+1, 2):
             qml.CZ(wires=[wire, (wire + 1)%self.width])
def entangle_layer(width=4, layer=4):
    """adds entangling gates to even / odd wires"""
    even = [[i, i+1] for i in range(0, width, 2)]
    odd = [[i+1, (i+2)%width] for i in range(0, width, 2)]
    for i in range(int(width/2)):
        if layer%2 == 0:
            qml.CZ(wires=even[i])
        if layer%2 == 1:
            qml.CZ(wires=odd[i])
def circuit1(params, x=None):
    for i in range(n_wires):
        qml.RX(x[i % n_features], wires=i)
        qml.Rot(*params[0, 0, i], wires=i)

    qml.CZ(wires=[0, 1])
    qml.CZ(wires=[1, 2])
    qml.CZ(wires=[1, 3])

    for i in range(n_wires):
        qml.Rot(*params[0, 1, i], wires=i)
    return qml.expval(qml.PauliZ(0)), qml.expval(qml.PauliZ(1)), qml.expval(qml.PauliZ(2))
Exemple #8
0
def plain_variational_circuit(params):
    layers = params.shape[0]
    wires = params.shape[1]
    for layer in range(layers):
        for wire in range(wires):
            qml.RX(params[layer][wire][0], wires=wire)
            qml.RY(params[layer][wire][1], wires=wire)
        for wire in range(0, layers - 1, 2):
            qml.CZ(wires=[wire, wire + 1])
        for wire in range(1, layers - 1, 2):
            qml.CZ(wires=[wire, wire + 1])
    return qml.probs(wires=range(wires))
Exemple #9
0
    def rand_circuit(self, cparams, num_qparams):
        # Numbers of classical and "quantum" parameters
        gate_idx = 0
        num_cparams = self.num_params - num_qparams

        # Wires:
        #   First `num_qubits` wires form the main register.
        #   After that, every `precision_qparams` wires coresponds to one qparam.

        num_wires = self.num_qubits + num_qparams * self.precision_qparams

        # Iterate over layers
        for k in range(self.num_params):
            # Algorithm description: the number of layers in this circuit is
            # equal to num_params, with each layer receiving a single parameter.
            # The first layers will be filled with classical parameters, after
            # which the remainder of layers receive quantum parameters.

            # sqrt(H) mixing layer
            for i in range(self.num_qubits):
                qml.RY(np.pi / 4, wires=i)

            # Fill out classical parameter first
            if k < num_cparams:
                # First wire gets the param
                self.gate_set[self.random_gateidx_sequence[k]](cparams[k],
                                                               wires=0)
            # Fill out quantum parameters
            else:
                ctrl_gate = self.ctrl_gate_set[self.random_gateidx_sequence[k]]
                # Add controls to every wire in the ancilla register according
                # to the bit of theta that we're capturing.
                for j in range(self.precision_qparams):
                    ctrl_wire = self.num_qubits + (
                        k - num_cparams) * self.precision_qparams + j

                    #j = 0 is the least significant
                    ctrl_angle = self.max_qparams * 2**(
                        self.precision_qparams - j - 1) / self.num_bins

                    ctrl_gate(ctrl_angle, wires=[ctrl_wire, 0])
            # Pad the remainder of each parameterized layer with random paulis
            for i in range(1, self.num_qubits):
                rand_angle = np.random.random() * np.pi
                rand_gate = self.gate_set[self.singleq_gate_sequence[gate_idx]]
                gate_idx += 1
                rand_gate(rand_angle, wires=i)

            # Interleaved entangling gates
            for i in range(0, self.num_qubits - 1, 2):
                qml.CZ(wires=[i, i + 1])
            for i in range(1, self.num_qubits - 1, 2):
                qml.CZ(wires=[i, i + 1])
def qfunc(theta):
    qml.Hadamard(wires=0)
    qml.PauliX(wires=1)
    qml.S(wires=1)
    qml.adjoint(qml.S)(wires=1)
    qml.Hadamard(wires=0)
    qml.CNOT(wires=[0, 1])
    qml.RZ(theta[0], wires=2)
    qml.PauliX(wires=1)
    qml.CZ(wires=[1, 0])
    qml.RY(theta[1], wires=2)
    qml.CZ(wires=[0, 1])
    return qml.expval(qml.PauliX(0) @ qml.PauliX(2))
def circuit_decomposed(initial_weights, weights):
    qml.RY(initial_weights[0], wires=0)
    qml.RY(initial_weights[1], wires=1)
    qml.RY(initial_weights[2], wires=2)

    qml.CZ(wires=[0, 1])
    qml.RY(weights[0, 0, 0], wires=0)
    qml.RY(weights[0, 0, 1], wires=1)

    qml.CZ(wires=[1, 2])
    qml.RY(weights[0, 1, 0], wires=1)
    qml.RY(weights[0, 1, 1], wires=2)

    return qml.expval(qml.PauliZ(0))
Exemple #12
0
def variational_circuit(params, masked_circuit: MaskedCircuit = None):
    full_parameters = masked_circuit.expanded_parameters(params)
    for layer, layer_hidden in enumerate(masked_circuit.mask(Axis.LAYERS)):
        if not layer_hidden:
            for wire, wire_hidden in enumerate(masked_circuit.mask(Axis.WIRES)):
                if not wire_hidden:
                    if not masked_circuit.mask(Axis.PARAMETERS)[layer][wire][0]:
                        qml.RX(full_parameters[layer][wire][0], wires=wire)
                    if not masked_circuit.mask(Axis.PARAMETERS)[layer][wire][1]:
                        qml.RY(full_parameters[layer][wire][1], wires=wire)
            for wire in range(0, masked_circuit.mask(Axis.LAYERS).size - 1, 2):
                qml.CZ(wires=[wire, wire + 1])
            for wire in range(1, masked_circuit.mask(Axis.LAYERS).size - 1, 2):
                qml.CZ(wires=[wire, wire + 1])
    return qml.probs(wires=range(len(masked_circuit.mask(Axis.WIRES))))
Exemple #13
0
    def circuit(params, x):
        xEmbeded = [i * np.pi for i in x]
        for i in range(NUM_WIRES):
            qml.RX(xEmbeded[i], wires=i)
            qml.Rot(*params[0, i], wires=i)

        qml.CZ(wires=[1, 0])
        qml.CZ(wires=[1, 2])
        qml.CZ(wires=[0, 2])

        for i in range(NUM_WIRES):
            qml.Rot(*params[1, i], wires=i)

        return qml.expval(qml.PauliZ(0)), qml.expval(
            qml.PauliZ(1)), qml.expval(qml.PauliZ(2))
Exemple #14
0
 def decomposition(wires):
     return [
         qml.SWAP(wires=wires),
         qml.S(wires=[wires[0]]),
         qml.S(wires=[wires[1]]),
         qml.CZ(wires=wires),
     ]
def variational_ansatz(params, wires):
    """The variational ansatz circuit.

    Fill in the details of your ansatz between the # QHACK # comment markers. Your
    ansatz should produce an n-qubit state of the form

        a_0 |10...0> + a_1 |01..0> + ... + a_{n-2} |00...10> + a_{n-1} |00...01>

    where {a_i} are real-valued coefficients.

    Args:
         params (np.array): The variational parameters.
         wires (qml.Wires): The device wires that this circuit will run on.
    """

    # QHACK #
    N = len(wires)

    qml.PauliX(wires=0)

    for i, param in enumerate(params):
        qml.RY(-param, wires=i + 1)
        qml.CZ(wires=[i + 1, i])
        qml.RY(param, wires=i + 1)

    # Reversed CNOTs
    for i in range(len(params)):
        qml.Hadamard(wires=i)
        qml.Hadamard(wires=i + 1)
        qml.CNOT(wires=[i, i + 1])
        qml.Hadamard(wires=i)
        qml.Hadamard(wires=i + 1)
Exemple #16
0
    def rand_circuit(self, cparams, num_qparams):
        # Numbers of classical and "quantum" parameters
        num_cparams = self.num_params - num_qparams

        # Wires:
        #   First `num_qubits` wires form the main register.
        #   After that, every `precision_qparams` wires coresponds to one qparam.

        num_wires = self.num_qubits + num_qparams * self.precision_qparams

        for i in range(self.num_qubits):
            qml.RY(np.pi / 4, wires=i)

        for i in range(num_cparams):
            self.gate_set[self.random_gateidx_sequence[i]](cparams[i], wires=i)

        for i in range(num_cparams, self.num_params):
            ctrl_gate = self.ctrl_gate_set[self.random_gateidx_sequence[i]]
            for j in range(self.precision_qparams):
                ctrl_wire = self.num_qubits + (
                    i - num_cparams) * self.precision_qparams + j

                #j = 0 is the least significant
                ctrl_angle = self.max_qparams * 2**(self.precision_qparams -
                                                    j - 1) / self.num_bins

                ctrl_gate(ctrl_angle, wires=[ctrl_wire, i])

        for i in range(self.num_qubits - 1):
            qml.CZ(wires=[i, i + 1])
Exemple #17
0
 def circuit():
     """A combination of two and three qubit gates with the one_qubit_block and a simple
     PauliZ measurement, all acting on a four qubit input basis state"""
     qml.BasisState(np.array(basis_state), wires=[0, 1, 2, 3])
     qml.RX(0.5, wires=0)
     qml.Hadamard(wires=1)
     qml.RY(0.9, wires=2)
     qml.Rot(0.1, -0.2, -0.3, wires=3)
     qml.CNOT(wires=[0, 1])
     qml.CNOT(wires=[3, 1])
     one_qubit_block(wires=3)
     qml.CNOT(wires=[2, 0])
     qml.CZ(wires=[1, 0])
     one_qubit_block(wires=0)
     qml.Toffoli(wires=[1, 0, 2])
     one_qubit_block(wires=2)
     qml.SWAP(wires=[0, 1])
     qml.SWAP(wires=[0, 2])
     qml.Toffoli(wires=[1, 3, 2])
     qml.CRX(0.5, wires=[1, 0])
     qml.CSWAP(wires=[2, 1, 0])
     qml.CRY(0.9, wires=[2, 1])
     one_qubit_block(wires=1)
     qml.CRZ(0.02, wires=[0, 1])
     qml.CRY(0.9, wires=[2, 3])
     qml.CRot(0.2, 0.3, 0.7, wires=[2, 1])
     qml.RZ(0.4, wires=0)
     qml.Toffoli(wires=[2, 1, 0])
     return qml.expval(qml.PauliZ(0))
    def test_statevector_complex_circuit(self, dev, tol):
        dy = np.array([[1.0, 0.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0],
                       [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 0.0, 1.0]])
        fn0 = dev.vjp([qml.state()], dy[0, :])
        fn1 = dev.vjp([qml.state()], dy[1, :])
        fn2 = dev.vjp([qml.state()], dy[2, :])
        fn3 = dev.vjp([qml.state()], dy[3, :])

        params = [math.pi / 7, 6 * math.pi / 7]

        with qml.tape.QuantumTape() as tape:
            qml.QubitStateVector(np.array([1.0] * 4) / 2, wires=range(2))
            qml.RY(params[0], wires=0)
            qml.RZ(params[1], wires=1)
            qml.CZ(wires=[0, 1])

        tape.trainable_params = {2}  # RZ

        psi_00_diff = (
            (math.cos(params[0] / 2) - math.sin(params[0] / 2)) *
            (-math.sin(params[1] / 2) - 1j * math.cos(params[1] / 2)) / 4)
        psi_01_diff = (
            (math.cos(params[0] / 2) + math.sin(params[0] / 2)) *
            (-math.sin(params[1] / 2) - 1j * math.cos(params[1] / 2)) / 4)
        psi_10_diff = (
            (math.cos(params[0] / 2) - math.sin(params[0] / 2)) *
            (-math.sin(params[1] / 2) + 1j * math.cos(params[1] / 2)) / 4)
        psi_11_diff = (
            -(math.cos(params[0] / 2) + math.sin(params[0] / 2)) *
            (-math.sin(params[1] / 2) + 1j * math.cos(params[1] / 2)) / 4)

        assert np.allclose(fn0(tape), psi_00_diff, atol=tol)
        assert np.allclose(fn1(tape), psi_01_diff, atol=tol)
        assert np.allclose(fn2(tape), psi_10_diff, atol=tol)
        assert np.allclose(fn3(tape), psi_11_diff, atol=tol)
Exemple #19
0
def local_hadamard_test(weights, l=None, lp=None, j=None, part=None):

    # First Hadamard gate applied to the ancillary qubit.
    qml.Hadamard(wires=ancilla_idx)

    # For estimating the imaginary part of the coefficient "mu", we must add a "-i" phase gate.
    if part == "Im" or part == "im":
        qml.PhaseShift(-np.pi / 2, wires=ancilla_idx)

    # Variational circuit generating a guess for the solution vector |x>
    variational_block(weights)

    # Controlled application of the unitary component A_l of the problem matrix A.
    CA(l)

    # Adjoint of the unitary U_b associated to the problem vector |b>.
    # In this specific example Adjoint(U_b) = U_b.
    U_b()

    # Controlled Z operator at position j. If j = -1, apply the identity.
    if j != -1:
        qml.CZ(wires=[ancilla_idx, j])

    # Unitary U_b associated to the problem vector |b>.
    U_b()

    # Controlled application of Adjoint(A_lp).
    # In this specific example Adjoint(A_lp) = A_lp.
    CA(lp)

    # Second Hadamard gate applied to the ancillary qubit.
    qml.Hadamard(wires=ancilla_idx)

    # Expectation value of Z for the ancillary qubit.
    return qml.expval(qml.PauliZ(wires=ancilla_idx))
Exemple #20
0
def test_control_sanity_check():
    """Test that control works on a very standard usecase."""
    def make_ops():
        qml.RX(0.123, wires=0)
        qml.RY(0.456, wires=2)
        qml.RX(0.789, wires=0)
        qml.Rot(0.111, 0.222, 0.333, wires=2),
        qml.PauliX(wires=2)
        qml.PauliY(wires=4)
        qml.PauliZ(wires=0)

    with QuantumTape() as tape:
        cmake_ops = ctrl(make_ops, control=1)
        #Execute controlled version.
        cmake_ops()

    expected = [
        qml.CRX(0.123, wires=[1, 0]),
        qml.CRY(0.456, wires=[1, 2]),
        qml.CRX(0.789, wires=[1, 0]),
        qml.CRot(0.111, 0.222, 0.333, wires=[1, 2]),
        qml.CNOT(wires=[1, 2]),
        qml.CY(wires=[1, 4]),
        qml.CZ(wires=[1, 0]),
    ]
    assert len(tape.operations) == 1
    ctrl_op = tape.operations[0]
    assert isinstance(ctrl_op, ControlledOperation)
    expanded = ctrl_op.expand()
    assert_equal_operations(expanded.operations, expected)
Exemple #21
0
def decompose_ua(phi, wires=None):
    r"""Implements the circuit decomposing the controlled application of the unitary
    :math:`U_A(\phi)`

    .. math::

        U_A(\phi) = \left(\begin{array}{cc} 0 & e^{-i\phi} \\ e^{-i\phi} & 0 \\ \end{array}\right)

    in terms of the quantum operations supported by PennyLane.

    :math:`U_A(\phi)` is used in `arXiv:1805.04340 <https://arxiv.org/abs/1805.04340>`_,
    to define two-qubit exchange gates required to build particle-conserving
    VQE ansatze for quantum chemistry simulations. See :func:`~.ParticleConservingU1`.

    :math:`U_A(\phi)` is expressed in terms of ``PhaseShift``, ``Rot`` and ``PauliZ`` operations
    :math:`U_A(\phi) = R_\phi(-2\phi) R(-\phi, \pi, \phi) \sigma_z`.

    Args:
        phi (float): angle :math:`\phi` defining the unitary :math:`U_A(\phi)`
        wires (Iterable): the wires ``n`` and ``m`` the circuit acts on
    """

    n, m = wires

    qml.CZ(wires=wires)
    qml.CRot(-phi, np.pi, phi, wires=wires)

    # decomposition of C-PhaseShift(2*phi) gate
    qml.PhaseShift(-phi, wires=m)
    qml.CNOT(wires=wires)
    qml.PhaseShift(phi, wires=m)
    qml.CNOT(wires=wires)
    qml.PhaseShift(-phi, wires=n)
def CA_all():
    """Controlled application of all the unitary components A_l of the problem matrix A."""
    # Controlled-A_1
    qml.CNOT(wires=[ancilla_idx, 0])
    qml.CZ(wires=[ancilla_idx, 1])

    # Controlled-A2
    qml.CNOT(wires=[ancilla_idx + 1, 0])
Exemple #23
0
def random_circuit_stat(params, G=0, sequence=0, p=0, **kwargs):
    for i in range(len(G.nodes)):
        qml.RY(np.pi / 4, wires=i)
    for j in range(p):
        Random_unitary(params, G, sequence)
    for i in range(len(G.nodes) - 1):
        qml.CZ(wires=[i, i + 1])
    return [qml.sample(qml.PauliZ(i)) for i in range(len(G.nodes))]
Exemple #24
0
def random_circuit(params, G=0, sequence=0, p=0, **kwargs):
    for i in range(len(G.nodes)):
        qml.RY(np.pi / 4, wires=i)
    for j in range(p):
        Random_unitary(params, G, sequence)
    for i in range(len(G.nodes) - 1):
        qml.CZ(wires=[i, i + 1])
    return qml.probs(wires=list(range(len(G.nodes))))
Exemple #25
0
def Odd(thetaOdd):
    """ Applies a set of RZ(theta) followed by CZ entanglement between every pair of qubits
    """
    for wire in range(n_wires):
        qml.RZ(thetaOdd[wire], wires=wire)
        for i in range(n_wires):
            for j in range(i + 1, n_wires):
                qml.CZ(wires=[i, j])
Exemple #26
0
 def convert_cnots(tape):
     for op in tape.operations + tape.measurements:
         if op.name == "CNOT":
             wires = op.wires
             qml.Hadamard(wires=wires[0])
             qml.CZ(wires=[wires[0], wires[1]])
         else:
             op.queue()
def Turing():
    # distinct fun names in functional placeholders=
    # []
    qml.Hadamard(0)
    qml.PauliX(1)
    qml.PauliY(1)
    qml.PauliZ(1)
    qml.CNOT(wires=[0, 1])
    qml.CZ(wires=[0, 1])
    qml.SWAP(wires=[1, 0])
    qml.RX(-6.283185307179586, wires=2)
    qml.RY(-6.283185307179586, wires=2)
    qml.RZ(-6.283185307179586, wires=2)
    qml.PhaseShift(3.141592653589793, wires=1)

    def rot(rad_ang_x, rad_ang_y, rad_ang_z):
        """
        Returns
    
        exp(1j*(rad_ang_x*sig_x + rad_ang_y*sig_y + rad_ang_z*sig_z))
    
        where rad_ang_x is an angle in radians and sig_x is the x Pauli
        matrix, etc.
    
        Parameters
        ----------
        rad_ang_x : float
        rad_ang_y : float
        rad_ang_z : float
    
        Returns
        -------
        np.ndarray
    
        """
        ty = np.complex128
        mat = np.zeros([2, 2], dtype=ty)
        vec = np.array([rad_ang_x, rad_ang_y, rad_ang_z])
        n = np.linalg.norm(vec)  # sqrt(dot(vec, vec.conj))
        if abs(n) < 1e-8:
            mat[0, 0] = 1
            mat[1, 1] = 1
        else:
            nx = rad_ang_x / n
            ny = rad_ang_y / n
            nz = rad_ang_z / n
            c = np.cos(n)
            s = np.sin(n)
            mat[0, 0] = c + 1j * s * nz
            mat[0, 1] = s * ny + 1j * s * nx
            mat[1, 0] = -s * ny + 1j * s * nx
            mat[1, 1] = c - 1j * s * nz
        return mat

    qml.QubitUnitary(rot(-6.283185307179586, -6.283185307179586,
                         -6.283185307179586),
                     wires=0)
    return qml.expval.Hermitian(hamil)
Exemple #28
0
 def my_transform(tape):
     for op in tape.operations + tape.measurements:
         if op.name == "CRX":
             wires = op.wires
             param = op.parameters[0]
             qml.RX(param, wires=wires[1])
             qml.RY(param / 2, wires=wires[1])
             qml.CZ(wires=[wires[1], wires[0]])
         else:
             op.queue()
def parameterized_qubit_tape():
    """A parametrized qubit ciruit."""
    a, b, c = 0.1, 0.2, 0.3
    angles = np.array([0.4, 0.5, 0.6])

    with qml.tape.QuantumTape() as tape:
        qml.RX(a, wires=0)
        qml.RX(b, wires=1)
        qml.PauliZ(1)
        qml.CNOT(wires=[0, 1]).inv()
        qml.CRY(b, wires=[3, 1])
        qml.RX(angles[0], wires=0)
        qml.RX(4 * angles[1], wires=1)
        qml.PhaseShift(17 / 9 * c, wires=2)
        qml.RZ(b, wires=3)
        qml.RX(angles[2], wires=2).inv()
        qml.CRY(0.3589, wires=[3, 1]).inv()
        qml.CSWAP(wires=[4, 2, 1]).inv()
        qml.QubitUnitary(np.eye(2), wires=[2])
        qml.ControlledQubitUnitary(np.eye(2), control_wires=[0, 1], wires=[2])
        qml.MultiControlledX(control_wires=[0, 1, 2], wires=[3])
        qml.Toffoli(wires=[0, 2, 1])
        qml.CNOT(wires=[0, 2])
        qml.PauliZ(wires=[1])
        qml.PauliZ(wires=[1]).inv()
        qml.CZ(wires=[0, 1])
        qml.CZ(wires=[0, 2]).inv()
        qml.CY(wires=[1, 2])
        qml.CY(wires=[2, 0]).inv()
        qml.CNOT(wires=[2, 1])
        qml.CNOT(wires=[0, 2])
        qml.SWAP(wires=[0, 2]).inv()
        qml.CNOT(wires=[1, 3])
        qml.RZ(b, wires=3)
        qml.CSWAP(wires=[4, 0, 1])

        qml.expval(qml.PauliY(0)),
        qml.var(qml.Hadamard(wires=1)),
        qml.sample(qml.PauliX(2)),
        qml.expval(qml.Hermitian(np.eye(4), wires=[3, 4])),

    return tape
        def qfunc():
            qml.CZ(wires=[0, 2])
            qml.PauliZ(wires=2)
            qml.S(wires=0)

            qml.CNOT(wires=[0, 1])

            qml.CRZ(0.5, wires=[0, 1])
            qml.RZ(0.2, wires=2)
            qml.T(wires=0)
            qml.PauliZ(wires=0)