def test_circuit_from_quil():
    """Convert a test circuit from Quil with a wide range of gates."""
    q0, q1, q2 = LineQubit.range(3)
    cirq_circuit = Circuit(
        [
            I(q0),
            I(q1),
            I(q2),
            X(q0),
            Y(q1),
            Z(q2),
            H(q0),
            S(q1),
            T(q2),
            ZPowGate(exponent=1 / 8)(q0),
            ZPowGate(exponent=1 / 8)(q1),
            ZPowGate(exponent=1 / 8)(q2),
            XPowGate(exponent=1 / 2, global_shift=-0.5)(q0),
            YPowGate(exponent=1 / 2, global_shift=-0.5)(q1),
            ZPowGate(exponent=1 / 2, global_shift=-0.5)(q2),
            CZ(q0, q1),
            CNOT(q1, q2),
            CZPowGate(exponent=1 / 2, global_shift=0.0)(q0, q1),
            CPHASE00(phi=np.pi / 2)(q1, q2),
            CPHASE01(phi=np.pi / 2)(q0, q1),
            CPHASE10(phi=np.pi / 2)(q1, q2),
            ISWAP(q0, q1),
            PSWAP(phi=np.pi / 2)(q1, q2),
            SWAP(q0, q1),
            ISwapPowGate(exponent=1 / 2, global_shift=0.0)(q1, q2),
            CCNOT(q0, q1, q2),
            CSWAP(q0, q1, q2),
            MeasurementGate(1, key="ro[0]")(q0),
            MeasurementGate(1, key="ro[1]")(q1),
            MeasurementGate(1, key="ro[2]")(q2),
        ]
    )
    # build the same Circuit, using Quil
    quil_circuit = circuit_from_quil(Program(QUIL_PROGRAM))
    # test Circuit equivalence
    assert cirq_circuit == quil_circuit

    pyquil_circuit = Program(QUIL_PROGRAM)
    # drop declare and measures, get Program unitary
    pyquil_unitary = program_unitary(pyquil_circuit[1:-3], n_qubits=3)
    # fix qubit order convention
    cirq_circuit_swapped = Circuit(SWAP(q0, q2), cirq_circuit[:-1], SWAP(q0, q2))
    # get Circuit unitary
    cirq_unitary = cirq_circuit_swapped.unitary()
    # test unitary equivalence
    assert np.isclose(pyquil_unitary, cirq_unitary).all()
Convert a test circuit from Quil with a wide range of gates.
cirq-rigetti/cirq_rigetti/quil_input_test.py