Ejemplo n.º 1
0
def test_to_from_pennylane_identity():
    q = cirq.LineQubit(0)
    # Empty circuit
    circuit = cirq.Circuit()
    converted = from_pennylane(to_pennylane(circuit))
    assert _equal(circuit, converted, require_qubit_equality=False)
    circuit = cirq.Circuit(cirq.I(q))
    # Identity gate
    converted = from_pennylane(to_pennylane(circuit))
    # TODO: test circuit equality after Identity operation will be added
    # to PennyLane (https://github.com/PennyLaneAI/pennylane/issues/1632)
    assert np.allclose(cirq.unitary(circuit), cirq.unitary(converted))
Ejemplo n.º 2
0
def test_to_from_pennylane(random_state):
    circuit = cirq.testing.random_circuit(
        qubits=4, n_moments=2, op_density=1, random_state=random_state
    )

    converted = from_pennylane(to_pennylane(circuit))
    # Gates (e.g. iSWAP) aren't guaranteed to be preserved. Check unitary
    # instead of circuit equality.
    cirq.testing.assert_allclose_up_to_global_phase(
        cirq.unitary(converted), cirq.unitary(circuit), atol=1e-7
    )
Ejemplo n.º 3
0
def test_integration():
    n_wires = 4

    gates = [
        qml.PauliX,
        qml.PauliY,
        qml.PauliZ,
        qml.S,
        qml.T,
        qml.RX,
        qml.RY,
        qml.RZ,
        qml.Hadamard,
        qml.Rot,
        qml.CRot,
        qml.Toffoli,
        qml.SWAP,
        qml.CSWAP,
        qml.U1,
        qml.U2,
        qml.U3,
        qml.CRX,
        qml.CRY,
        qml.CRZ,
    ]

    layers = 3
    np.random.seed(1967)
    gates_per_layers = [np.random.permutation(gates) for _ in range(layers)]

    with qml.tape.QuantumTape() as tape:
        np.random.seed(1967)
        for gates in gates_per_layers:
            for gate in gates:
                params = list(np.pi * np.random.rand(gate.num_params))
                rnd_wires = np.random.choice(range(n_wires),
                                             size=gate.num_wires,
                                             replace=False)
                gate(
                    *params,
                    wires=[
                        int(w) for w in rnd_wires
                    ],  # make sure we do not address wires as 0-d arrays
                )

    base_circ = from_pennylane(tape)
    tape_recovered = to_pennylane(base_circ)
    circ_recovered = from_pennylane(tape_recovered)

    u_1 = cirq.unitary(base_circ)
    u_2 = cirq.unitary(circ_recovered)

    assert np.allclose(u_1, u_2)
Ejemplo n.º 4
0
def test_integration():
    gates = [
        qml.PauliX(wires=0),
        qml.PauliY(wires=0),
        qml.PauliZ(wires=0),
        qml.S(wires=0),
        qml.T(wires=0),
        qml.RX(0.4, wires=0),
        qml.RY(0.4, wires=0),
        qml.RZ(0.4, wires=0),
        qml.Hadamard(wires=0),
        qml.Rot(0.4, 0.5, 0.6, wires=1),
        qml.CRot(0.4, 0.5, 0.6, wires=(0, 1)),
        qml.Toffoli(wires=(0, 1, 2)),
        qml.SWAP(wires=(0, 1)),
        qml.CSWAP(wires=(0, 1, 2)),
        qml.U1(0.4, wires=0),
        qml.U2(0.4, 0.5, wires=0),
        qml.U3(0.4, 0.5, 0.6, wires=0),
        qml.CRX(0.4, wires=(0, 1)),
        qml.CRY(0.4, wires=(0, 1)),
        qml.CRZ(0.4, wires=(0, 1)),
    ]

    layers = 3
    np.random.seed(1967)
    gates_per_layers = [np.random.permutation(gates) for _ in range(layers)]

    with qml.tape.QuantumTape() as tape:
        np.random.seed(1967)
        for gates in gates_per_layers:
            for gate in gates:
                qml.apply(gate)

    base_circ = from_pennylane(tape)
    tape_recovered = to_pennylane(base_circ)
    circ_recovered = from_pennylane(tape_recovered)
    u_1 = cirq.unitary(base_circ)
    u_2 = cirq.unitary(circ_recovered)
    cirq.testing.assert_allclose_up_to_global_phase(u_1, u_2, atol=0)
Ejemplo n.º 5
0
def test_to_from_pennylane_cnot_same_gates():
    qreg = cirq.LineQubit.range(2)
    circuit = cirq.Circuit(cirq.CNOT(*qreg))
    converted = from_pennylane(to_pennylane(circuit))
    assert _equal(circuit, converted, require_qubit_equality=False)