Beispiel #1
0
    def test_gradient(self, theta, phi, varphi, shots, tol):
        """Test that the gradient works correctly"""
        qc = QuantumCircuit(3)
        qiskit_params = [Parameter("param_{}".format(i)) for i in range(3)]

        qc.rx(qiskit_params[0], 0)
        qc.rx(qiskit_params[1], 1)
        qc.rx(qiskit_params[2], 2)
        qc.cx(0, 1)
        qc.cx(1, 2)

        # convert to a PennyLane circuit
        qc_pl = qml.from_qiskit(qc)

        dev = qml.device("default.qubit", wires=3, shots=shots)

        @qml.qnode(dev)
        def circuit(params):
            qiskit_param_mapping = dict(map(list, zip(qiskit_params, params)))
            qc_pl(qiskit_param_mapping)
            return qml.expval(qml.PauliX(0) @ qml.PauliY(2))

        dcircuit = qml.grad(circuit, 0)
        res = dcircuit([theta, phi, varphi])
        expected = [
            np.cos(theta) * np.sin(phi) * np.sin(varphi),
            np.sin(theta) * np.cos(phi) * np.sin(varphi),
            np.sin(theta) * np.sin(phi) * np.cos(varphi),
        ]

        assert np.allclose(res, expected, **tol)
Beispiel #2
0
def qnode(inputs, weights):
    inputs = inputs.numpy()
    weights = weights.numpy()
    input_list = []
    for i in range(1, size):
        input_list.append(inputs[i] - inputs[0])
    # run qiskit circuit
    qc.bind_parameters({
        theta1: input_list[0],
        theta2: input_list[1],
        theta3: input_list[2],
        w1: weights[0],
        w2: weights[1],
        w3: weights[2]
    })
    qml.from_qiskit(qc)
    # measure ancilla-qubit
    # TODO: Try out whether expectation value (expval) or sample value (sample) of the qubit works better
    return qml.expval(qml.PauliZ(ancilla))
    def test_differentiable_param_is_array(self, analytic, tol):
        """Test that extracting the differentiable parameters works correctly
        for arrays"""
        qc = QuantumCircuit(3)
        qiskit_params = [Parameter("param_{}".format(i)) for i in range(3)]

        theta = 0.53
        phi = -1.23
        varphi = 0.8654
        params = [
            qml.numpy.tensor(theta),
            qml.numpy.tensor(phi),
            qml.numpy.tensor(varphi)
        ]

        qc.rx(qiskit_params[0], 0)
        qc.rx(qiskit_params[1], 1)
        qc.rx(qiskit_params[2], 2)
        qc.cx(0, 1)
        qc.cx(1, 2)

        # convert to a PennyLane circuit
        qc_pl = qml.from_qiskit(qc)

        dev = qml.device("default.qubit", wires=3, analytic=analytic)

        @qml.qnode(dev)
        def circuit(params):
            qiskit_param_mapping = dict(map(list, zip(qiskit_params, params)))
            qc_pl(qiskit_param_mapping)
            return qml.expval(qml.PauliX(0) @ qml.PauliY(2))

        dcircuit = qml.grad(circuit, 0)
        res = dcircuit(params)
        expected = [
            np.cos(theta) * np.sin(phi) * np.sin(varphi),
            np.sin(theta) * np.cos(phi) * np.sin(varphi),
            np.sin(theta) * np.sin(phi) * np.cos(varphi)
        ]

        assert np.allclose(res, expected, **tol)
Beispiel #4
0
    def get_wavefunction(self, circuit):
        """
        Returns a wavefunction representing quantum state produced by a circuit

        Args:
            circuit (core.circuit.Circuit): quantum circuit to be executed.

        Returns:
            pyquil.Wafefunction: wavefunction object.

        """
        num_qubits = len(circuit.get_qubits())
        dev = qml.device(self.device, wires=num_qubits)

        ansatz = qml.from_qiskit(circuit.to_qiskit())

        @qml.qnode(dev)
        def _circuit():
            ansatz(wires=list(range(num_qubits)))
            return qml.expval(qml.Identity(0))

        _circuit()
        return Wavefunction(dev.state)
Beispiel #5
0
    def get_exact_expectation_values(self, circuit, qubit_operator, **kwargs):
        """
        Calculates the expectation values for given operator, based on the exact quantum state produced by circuit.

        Args:
            circuit (core.circuit.Circuit): quantum circuit to be executed.
            qubit_operator (openfermion): Operator for which we calculate the expectation value.

        Returns:
            ExpectationValues: object representing expectation values for given operator.
        """
        # convert the OpenFermion operator to PennyLane observables
        coeffs, obs = qml.qchem.convert_hamiltonian(qubit_operator).terms
        num_qubits = len(circuit.get_qubits())
        dev = qml.device(self.device, wires=num_qubits)

        ansatz = qml.from_qiskit(circuit.to_qiskit())
        qnodes = qml.map(lambda weights, **kwargs: ansatz(**kwargs),
                         obs,
                         dev,
                         measure="expval")
        return ExpectationValues(
            values=[coeff * qnode([]) for coeff, qnode in zip(coeffs, qnodes)])
 def circuit_function():
     qml.from_qiskit(circuit)()
     return qml.expval(qml.PauliZ(0))