Beispiel #1
0
    def test_quantum_circuit_with_multiple_parameters(self, recorder):
        """Tests loading a circuit with multiple parameters."""

        angle1 = 0.5
        angle2 = 0.3

        phi = Parameter("φ")
        theta = Parameter("θ")

        qc = QuantumCircuit(3, 1)
        qc.rx(phi, 1)
        qc.rz(theta, 0)

        quantum_circuit = load(qc)

        with recorder:
            quantum_circuit(params={phi: angle1, theta: angle2})

        assert len(recorder.queue) == 2
        assert recorder.queue[0].name == "RX"
        assert recorder.queue[0].parameters == [angle1]
        assert recorder.queue[0].wires == Wires([1])
        assert recorder.queue[1].name == "RZ"
        assert recorder.queue[1].parameters == [angle2]
        assert recorder.queue[1].wires == Wires([0])
    def test_cry(self, recorder):
        """Tests that the decomposition of the controlled-Y operation is being
        loaded."""
        # This test will be merged into the test_controlled_rotations test once
        # the native CRY is used in Qiskit and not a set of instructions
        # yielded from decomposition is being added

        q2 = QuantumRegister(2)
        qc = QuantumCircuit(q2)

        qc.cry(0.5, q2[0], q2[1])

        quantum_circuit = load(qc)

        with recorder:
            quantum_circuit()

        assert len(recorder.queue) == 4
        assert recorder.queue[0].name == "U3"
        assert recorder.queue[0].params == [0.25, 0, 0]
        assert recorder.queue[0].wires == [1]

        assert recorder.queue[1].name == "CNOT"
        assert recorder.queue[1].wires == [0, 1]

        assert recorder.queue[2].name == "U3"
        assert recorder.queue[2].params == [-0.25, 0, 0]
        assert recorder.queue[2].wires == [1]

        assert recorder.queue[3].name == "CNOT"
        assert recorder.queue[3].wires == [0, 1]
Beispiel #3
0
    def test_two_qubit_parametrized_operations_supported_by_pennylane(self, recorder):
        """Tests loading a circuit with the two-qubit parametrized operations supported by PennyLane."""

        two_wires = [0, 1]
        angle = 0.3333

        qc = QuantumCircuit(2, 1)

        qc.crz(angle, *two_wires)
        qc.rzz(angle, *two_wires)
        qc.ryy(angle, *two_wires)
        qc.rxx(angle, *two_wires)

        quantum_circuit = load(qc)
        with recorder:
            quantum_circuit()

        assert len(recorder.queue) == 4

        assert recorder.queue[0].name == "CRZ"
        assert recorder.queue[0].parameters == [angle]
        assert recorder.queue[0].wires == Wires(two_wires)

        assert recorder.queue[1].name == "IsingZZ"
        assert recorder.queue[1].parameters == [angle]
        assert recorder.queue[1].wires == Wires(two_wires)

        assert recorder.queue[2].name == "IsingYY"
        assert recorder.queue[2].parameters == [angle]
        assert recorder.queue[2].wires == Wires(two_wires)

        assert recorder.queue[3].name == "IsingXX"
        assert recorder.queue[3].parameters == [angle]
        assert recorder.queue[3].wires == Wires(two_wires)
Beispiel #4
0
    def test_quantum_circuit_loaded_multiple_times_with_different_arguments(self, recorder):
        """Tests that a loaded quantum circuit can be called multiple times with
        different arguments."""

        theta = Parameter("θ")
        angle1 = 0.5
        angle2 = -0.5
        angle3 = 0

        qc = QuantumCircuit(3, 1)
        qc.rz(theta, [0])

        quantum_circuit = load(qc)

        with recorder:
            quantum_circuit(params={theta: angle1})
            quantum_circuit(params={theta: angle2})
            quantum_circuit(params={theta: angle3})

        assert len(recorder.queue) == 3
        assert recorder.queue[0].name == "RZ"
        assert recorder.queue[0].parameters == [angle1]
        assert recorder.queue[0].wires == Wires([0])
        assert recorder.queue[1].name == "RZ"
        assert recorder.queue[1].parameters == [angle2]
        assert recorder.queue[1].wires == Wires([0])
        assert recorder.queue[2].name == "RZ"
        assert recorder.queue[2].parameters == [angle3]
        assert recorder.queue[2].wires == Wires([0])
Beispiel #5
0
    def test_loaded_quantum_circuit_and_further_pennylane_operations(self, recorder):
        """Tests that a loaded quantum circuit can be used around other PennyLane
        templates in a circuit."""

        theta = Parameter("θ")
        angle = 0.5

        qc = QuantumCircuit(3, 1)
        qc.rz(theta, [0])

        quantum_circuit = load(qc)

        with recorder:
            qml.PauliZ(0)
            quantum_circuit(params={theta: angle})
            qml.Hadamard(0)

        assert len(recorder.queue) == 3
        assert recorder.queue[0].name == "PauliZ"
        assert recorder.queue[0].parameters == []
        assert recorder.queue[0].wires == Wires([0])
        assert recorder.queue[1].name == "RZ"
        assert recorder.queue[1].parameters == [angle]
        assert recorder.queue[1].wires == Wires([0])
        assert recorder.queue[2].name == "Hadamard"
        assert recorder.queue[2].parameters == []
        assert recorder.queue[2].wires == Wires([0])
    def test_quantum_circuit_with_gate_requiring_multiple_parameters(
            self, recorder):
        """Tests loading a circuit containing a gate that requires
        multiple parameters."""

        angle1 = 0.5
        angle2 = 0.3
        angle3 = 0.1

        phi = Parameter('φ')
        lam = Parameter('λ')
        theta = Parameter('θ')

        qc = QuantumCircuit(3, 1)
        qc.u3(phi, lam, theta, [0])

        quantum_circuit = load(qc)

        with recorder:
            quantum_circuit(params={phi: angle1, lam: angle2, theta: angle3})

        assert recorder.queue[0].name == 'U3'
        assert len(recorder.queue[0].params) == 3
        assert recorder.queue[0].params == [0.5, 0.3, 0.1]
        assert recorder.queue[0].wires == [0]
Beispiel #7
0
    def test_quantum_circuit_error_not_qiskit_circuit_passed(self, recorder):
        """Tests the load method raises a ValueError, if something
        that is not a QuanctumCircuit was passed."""

        qc = qml.PauliZ(0)

        quantum_circuit = load(qc)

        with pytest.raises(ValueError):
            with recorder:
                quantum_circuit()
Beispiel #8
0
    def test_one_qubit_operations_supported_by_pennylane(self, recorder):
        """Tests loading a circuit with the one-qubit operations supported by PennyLane."""

        single_wire = [0]

        qc = QuantumCircuit(1, 1)
        qc.x(single_wire)
        qc.y(single_wire)
        qc.z(single_wire)
        qc.h(single_wire)
        qc.s(single_wire)
        qc.t(single_wire)
        qc.sx(single_wire)
        qc.id(single_wire)

        quantum_circuit = load(qc)
        with recorder:
            quantum_circuit()

        assert len(recorder.queue) == 8

        assert recorder.queue[0].name == "PauliX"
        assert recorder.queue[0].parameters == []
        assert recorder.queue[0].wires == Wires(single_wire)

        assert recorder.queue[1].name == "PauliY"
        assert recorder.queue[1].parameters == []
        assert recorder.queue[1].wires == Wires(single_wire)

        assert recorder.queue[2].name == "PauliZ"
        assert recorder.queue[2].parameters == []
        assert recorder.queue[2].wires == Wires(single_wire)

        assert recorder.queue[3].name == "Hadamard"
        assert recorder.queue[3].parameters == []
        assert recorder.queue[3].wires == Wires(single_wire)

        assert recorder.queue[4].name == "S"
        assert recorder.queue[4].parameters == []
        assert recorder.queue[4].wires == Wires(single_wire)

        assert recorder.queue[5].name == "T"
        assert recorder.queue[5].parameters == []
        assert recorder.queue[5].wires == Wires(single_wire)

        assert recorder.queue[6].name == "SX"
        assert recorder.queue[6].parameters == []
        assert recorder.queue[6].wires == Wires(single_wire)

        assert recorder.queue[7].name == "Identity"
        assert recorder.queue[7].parameters == []
        assert recorder.queue[7].wires == Wires(single_wire)
Beispiel #9
0
    def test_extra_parameters_were_passed(self, recorder):
        """Tests that loading raises an error when extra parameters were passed."""

        theta = Parameter('θ')
        phi = Parameter('φ')

        qc = QuantumCircuit(3, 1)

        quantum_circuit = load(qc)

        with pytest.raises(QiskitError):
            with recorder:
                quantum_circuit(params={theta: 0.5, phi: 0.3})
    def test_one_qubit_parametrized_operations_supported_by_pennylane(
            self, recorder):
        """Tests loading a circuit with the one-qubit parametrized operations supported by PennyLane."""

        single_wire = [0]
        angle = 0.3333

        phi = 0.3
        lam = 0.4
        theta = 0.2

        q_reg = QuantumRegister(1)
        qc = QuantumCircuit(q_reg)

        qc.u1(angle, single_wire)
        qc.rx(angle, single_wire)
        qc.ry(angle, single_wire)
        qc.rz(angle, single_wire)
        qc.u2(phi, lam, [0])
        qc.u3(phi, lam, theta, [0])

        quantum_circuit = load(qc)
        with recorder:
            quantum_circuit()

        assert recorder.queue[0].name == 'PhaseShift'
        assert recorder.queue[0].params == [angle]
        assert recorder.queue[0].wires == single_wire

        assert recorder.queue[1].name == 'RX'
        assert recorder.queue[1].params == [angle]
        assert recorder.queue[1].wires == single_wire

        assert recorder.queue[2].name == 'RY'
        assert recorder.queue[2].params == [angle]
        assert recorder.queue[2].wires == single_wire

        assert recorder.queue[3].name == 'RZ'
        assert recorder.queue[3].params == [angle]
        assert recorder.queue[3].wires == single_wire

        assert recorder.queue[4].name == 'U2'
        assert len(recorder.queue[4].params) == 2
        assert recorder.queue[4].params == [0.3, 0.4]
        assert recorder.queue[4].wires == [0]

        assert recorder.queue[5].name == 'U3'
        assert len(recorder.queue[5].params) == 3
        assert recorder.queue[5].params == [0.3, 0.4, 0.2]
        assert recorder.queue[5].wires == [0]
    def test_quantum_circuit_error_by_calling_wrong_parameters(self, recorder):
        """Tests that the load method for a QuantumCircuit raises a TypeError,
        if the wrong type of arguments were passed."""

        angle = 'some_string_instead_of_an_angle'

        qc = QuantumCircuit(3, 1)
        qc.rz(angle, [0])

        quantum_circuit = load(qc)

        with pytest.raises(TypeError,
                           match="parameter expected, got <class 'str'>"):
            with recorder:
                quantum_circuit()
Beispiel #12
0
    def test_quantum_circuit_error_by_passing_wrong_parameters(self, recorder):
        """Tests the load method for a QuantumCircuit raises a QiskitError,
        if the wrong type of arguments were passed."""

        theta = Parameter("θ")
        angle = np.tensor("some_string_instead_of_an_angle", requires_grad=False)

        qc = QuantumCircuit(3, 1)
        qc.rz(theta, [0])

        quantum_circuit = load(qc)

        with pytest.raises(QiskitError):
            with recorder:
                quantum_circuit(params={theta: angle})
Beispiel #13
0
    def test_parameter_was_not_bound(self, recorder):
        """Tests that loading raises an error when parameters were not bound."""

        theta = Parameter("θ")

        qc = QuantumCircuit(3, 1)
        qc.rz(theta, [0])

        quantum_circuit = load(qc)

        with pytest.raises(
            ValueError, match="The parameter {} was not bound correctly.".format(theta)
        ):
            with recorder:
                quantum_circuit(params={})
Beispiel #14
0
    def test_quantum_circuit_error_passing_parameters_not_required(self, recorder):
        """Tests the load method raises a QiskitError if arguments
        that are not required were passed."""

        theta = Parameter("θ")
        angle = np.tensor(0.5, requires_grad=False)

        qc = QuantumCircuit(3, 1)
        qc.z([0])

        quantum_circuit = load(qc)

        with pytest.raises(QiskitError):
            with recorder:
                quantum_circuit(params={theta: angle})
    def test_barrier_not_supported(self, recorder):
        """Tests that a warning is raised if an unsupported instruction was reached."""
        qc = QuantumCircuit(3, 1)
        qc.barrier()

        quantum_circuit = load(qc)

        with pytest.warns(UserWarning) as record:
            with recorder:
                quantum_circuit(params={})

        # check that only one warning was raised
        assert len(record) == 1
        # check that the message matches
        assert record[0].message.args[0] == "pennylane_qiskit.converter: The Barrier instruction is not supported by" \
                                            " PennyLane, and has not been added to the template."
Beispiel #16
0
    def test_operation_transformed_into_qubit_unitary(self, recorder):
        """Tests loading a circuit with operations that can be converted,
        but not natively supported by PennyLane."""

        qc = QuantumCircuit(3, 1)

        qc.ch([0], [1])

        quantum_circuit = load(qc)
        with recorder:
            quantum_circuit()

        assert recorder.queue[0].name == "QubitUnitary"
        assert len(recorder.queue[0].parameters) == 1
        assert np.array_equal(recorder.queue[0].parameters[0], ex.CHGate().to_matrix())
        assert recorder.queue[0].wires == Wires([0, 1])
Beispiel #17
0
    def test_quantum_circuit_error_parameter_not_bound(self, recorder):
        """Tests the load method for a QuantumCircuit raises a ValueError,
        if one of the parameters was not bound correctly."""

        theta = Parameter("θ")

        qc = QuantumCircuit(3, 1)
        qc.rz(theta, [0])

        quantum_circuit = load(qc)

        with pytest.raises(
            ValueError, match="The parameter {} was not bound correctly.".format(theta)
        ):
            with recorder:
                quantum_circuit()
Beispiel #18
0
    def test_crz(self, recorder):
        """Tests loading a circuit with the controlled-Z operation."""

        q2 = QuantumRegister(2)
        qc = QuantumCircuit(q2)
        qc.crz(0.5, q2[0], q2[1])

        quantum_circuit = load(qc)

        with recorder:
            quantum_circuit()

        assert len(recorder.queue) == 1
        assert recorder.queue[0].name == 'CRZ'
        assert recorder.queue[0].params == [0.5]
        assert recorder.queue[0].wires == [0, 1]
    def test_quantum_circuit_error_by_calling_wrong_parameters(self, recorder):
        """Tests that the load method for a QuantumCircuit raises a TypeError,
        if the wrong type of arguments were passed."""

        angle = 'some_string_instead_of_an_angle'

        qc = QuantumCircuit(3, 1)
        qc.rz(angle, [0])

        quantum_circuit = load(qc)

        with pytest.raises(
                ValueError,
                match="could not convert string to float: '{}'".format(angle)):
            with recorder:
                quantum_circuit()
Beispiel #20
0
    def test_extra_parameters_were_passed(self, recorder):
        """Tests that loading raises an error when extra parameters were
        passed."""

        theta = Parameter("θ")
        phi = Parameter("φ")
        x = np.tensor(0.5, requires_grad=False)
        y = np.tensor(0.3, requires_grad=False)

        qc = QuantumCircuit(3, 1)

        quantum_circuit = load(qc)

        with pytest.raises(QiskitError):
            with recorder:
                quantum_circuit(params={theta: x, phi: y})
Beispiel #21
0
    def test_pass_parameters_to_bind(self, recorder):
        """Tests parameter binding by passing parameters when loading a quantum circuit."""

        theta = Parameter("θ")

        qc = QuantumCircuit(3, 1)
        qc.rz(theta, [0])

        quantum_circuit = load(qc)

        with recorder:
            quantum_circuit(params={theta: 0.5})

        assert len(recorder.queue) == 1
        assert recorder.queue[0].name == "RZ"
        assert recorder.queue[0].parameters == [0.5]
        assert recorder.queue[0].wires == Wires([0])
Beispiel #22
0
    def test_controlled_rotations(self, qiskit_operation, pennylane_name, recorder):
        """Tests loading a circuit with two qubit Ising operations."""

        q2 = QuantumRegister(2)
        qc = QuantumCircuit(q2)

        qiskit_operation(qc, 0.5, q2[0], q2[1])

        quantum_circuit = load(qc)

        with recorder:
            quantum_circuit()

        assert len(recorder.queue) == 1
        assert recorder.queue[0].name == pennylane_name
        assert recorder.queue[0].parameters == [0.5]
        assert recorder.queue[0].wires == Wires([0, 1])
Beispiel #23
0
    def test_quantum_circuit_with_bound_parameters(self, recorder):
        """Tests loading a quantum circuit that already had bound parameters."""

        theta = Parameter("θ")

        qc = QuantumCircuit(3, 1)
        qc.rz(theta, [0])
        qc_1 = qc.bind_parameters({theta: 0.5})

        quantum_circuit = load(qc_1)

        with recorder:
            quantum_circuit()

        assert len(recorder.queue) == 1
        assert recorder.queue[0].name == "RZ"
        assert recorder.queue[0].parameters == [0.5]
        assert recorder.queue[0].wires == Wires([0])
    def test_invalid_parameter_expression(self, recorder):
        """Tests that an operation with multiple parameters raises an error."""

        theta = Parameter('θ')
        phi = Parameter('φ')

        qc = QuantumCircuit(3, 1)
        qc.rz(theta * phi, [0])

        quantum_circuit = load(qc)

        with pytest.raises(ValueError,
                           match='PennyLane does not support expressions'):
            with recorder:
                quantum_circuit(params={
                    theta: qml.variable.Variable(0),
                    phi: qml.variable.Variable(1)
                })
Beispiel #25
0
    def test_quantum_circuit_by_passing_parameters(self, recorder):
        """Tests the load method for a QuantumCircuit initialized by passing the number
        of registers required."""

        theta = Parameter("θ")
        angle = 0.5

        qc = QuantumCircuit(3, 1)
        qc.rz(theta, [0])

        quantum_circuit = load(qc)

        with recorder:
            quantum_circuit(params={theta: angle})

        assert len(recorder.queue) == 1
        assert recorder.queue[0].name == "RZ"
        assert recorder.queue[0].parameters == [angle]
        assert recorder.queue[0].wires == Wires([0])
Beispiel #26
0
    def test_operations_sdg_and_tdg(self, recorder):
        """Tests loading a circuit with the operations Sdg and Tdg gates."""

        qc = QuantumCircuit(3, 1)

        qc.sdg([0])
        qc.tdg([0])

        quantum_circuit = load(qc)
        with recorder:
            quantum_circuit()

        assert recorder.queue[0].name == "S.inv"
        assert len(recorder.queue[0].parameters) == 0
        assert recorder.queue[0].wires == Wires([0])

        assert recorder.queue[1].name == "T.inv"
        assert len(recorder.queue[1].parameters) == 0
        assert recorder.queue[1].wires == Wires([0])
Beispiel #27
0
    def test_one_qubit_parametrized_operations_supported_by_pennylane(self, recorder):
        """Tests loading a circuit with the one-qubit parametrized operations supported by PennyLane."""

        single_wire = [0]
        angle = 0.3333

        phi = 0.3
        lam = 0.4
        theta = 0.2

        q_reg = QuantumRegister(1)
        qc = QuantumCircuit(q_reg)

        qc.p(angle, single_wire)
        qc.rx(angle, single_wire)
        qc.ry(angle, single_wire)
        qc.rz(angle, single_wire)
        qc.u(phi, lam, theta, [0])

        quantum_circuit = load(qc)
        with recorder:
            quantum_circuit()

        assert recorder.queue[0].name == "PhaseShift"
        assert recorder.queue[0].parameters == [angle]
        assert recorder.queue[0].wires == Wires(single_wire)

        assert recorder.queue[1].name == "RX"
        assert recorder.queue[1].parameters == [angle]
        assert recorder.queue[1].wires == Wires(single_wire)

        assert recorder.queue[2].name == "RY"
        assert recorder.queue[2].parameters == [angle]
        assert recorder.queue[2].wires == Wires(single_wire)

        assert recorder.queue[3].name == "RZ"
        assert recorder.queue[3].parameters == [angle]
        assert recorder.queue[3].wires == Wires(single_wire)

        assert recorder.queue[4].name == "U3"
        assert len(recorder.queue[4].parameters) == 3
        assert recorder.queue[4].parameters == [0.3, 0.4, 0.2]
        assert recorder.queue[4].wires == Wires([0])
Beispiel #28
0
    def test_wires_two_different_quantum_registers(self, recorder):
        """Tests loading a circuit with the three-qubit operations supported by PennyLane."""

        three_wires = [0, 1, 2]

        qr1 = QuantumRegister(2)
        qr2 = QuantumRegister(1)

        qc = QuantumCircuit(qr1, qr2)

        qc.cswap(*three_wires)

        quantum_circuit = load(qc)
        with recorder:
            quantum_circuit()

        assert recorder.queue[0].name == "CSWAP"
        assert recorder.queue[0].parameters == []
        assert recorder.queue[0].wires == Wires(three_wires)
Beispiel #29
0
    def test_wires_pass_different_wires_than_for_circuit(self, recorder):
        """Tests that custom wires can be passed to the loaded template."""

        three_wires = [4, 7, 1]

        qr1 = QuantumRegister(2)
        qr2 = QuantumRegister(1)

        qc = QuantumCircuit(qr1, qr2)

        qc.cswap(*[0, 1, 2])

        quantum_circuit = load(qc)
        with recorder:
            quantum_circuit(wires=three_wires)

        assert recorder.queue[0].name == "CSWAP"
        assert recorder.queue[0].parameters == []
        assert recorder.queue[0].wires == Wires(three_wires)
    def test_one_qubit_operations_supported_by_pennylane(self, recorder):
        """Tests loading a circuit with the one-qubit operations supported by PennyLane."""

        single_wire = [0]

        qc = QuantumCircuit(1, 1)
        qc.x(single_wire)
        qc.y(single_wire)
        qc.z(single_wire)
        qc.h(single_wire)
        qc.s(single_wire)
        qc.t(single_wire)

        quantum_circuit = load(qc)
        with recorder:
            quantum_circuit()

        assert len(recorder.queue) == 6

        assert recorder.queue[0].name == 'PauliX'
        assert recorder.queue[0].params == []
        assert recorder.queue[0].wires == single_wire

        assert recorder.queue[1].name == 'PauliY'
        assert recorder.queue[1].params == []
        assert recorder.queue[1].wires == single_wire

        assert recorder.queue[2].name == 'PauliZ'
        assert recorder.queue[2].params == []
        assert recorder.queue[2].wires == single_wire

        assert recorder.queue[3].name == 'Hadamard'
        assert recorder.queue[3].params == []
        assert recorder.queue[3].wires == single_wire

        assert recorder.queue[4].name == 'S'
        assert recorder.queue[4].params == []
        assert recorder.queue[4].wires == single_wire

        assert recorder.queue[5].name == 'T'
        assert recorder.queue[5].params == []
        assert recorder.queue[5].wires == single_wire