Example #1
0
def test_recursive_composite():
    q0, q1 = QubitId(), QubitId()
    swap = SWAP(q0, q1)
    circuit = Circuit()
    circuit.append(swap)

    opt = ExpandComposite()
    opt.optimize_circuit(circuit)
    expected = Circuit().from_ops(
        Y(q1)**-0.5, CZ(q0, q1),
        Y(q1)**0.5,
        Y(q0)**-0.5, CZ(q1, q0),
        Y(q0)**0.5,
        Y(q1)**-0.5, CZ(q0, q1),
        Y(q1)**0.5)
    assert_equal_mod_empty(expected, circuit)
def test_recursive_composite_extension_overrides():
    q0, q1 = QubitId(), QubitId()
    swap = SWAP(q0, q1)
    circuit = cirq.Circuit()
    circuit.append(swap)
    ext = cirq.Extensions()
    ext.add_cast(cirq.CompositeGate, cirq.CNotGate, lambda _: OtherCNot())
    opt = cirq.ExpandComposite(composite_gate_extension=ext)
    opt.optimize_circuit(circuit)
    expected = cirq.Circuit()
    expected.append([Z(q0), Y(q1) ** -0.5, CZ(q0, q1), Y(q1) ** 0.5, Z(q0)])
    expected.append([Z(q1), Y(q0) ** -0.5, CZ(q1, q0), Y(q0) ** 0.5, Z(q1)],
                    strategy=cirq.InsertStrategy.INLINE)
    expected.append([Z(q0), Y(q1) ** -0.5, CZ(q0, q1), Y(q1) ** 0.5, Z(q0)],
                    strategy=cirq.InsertStrategy.INLINE)
    assert_equal_mod_empty(expected, circuit)
 def default_decompose(self, qubits):
     c, t = qubits
     yield Z(c)
     yield Y(t)**-0.5
     yield CZ(c, t)
     yield Y(t)**0.5
     yield Z(c)
def test_decompose_returns_deep_op_tree():
    class DummyGate(cirq.Gate, cirq.CompositeGate):
        def default_decompose(self, qubits):
            q0, q1 = qubits
            # Yield a tuple
            yield ((X(q0), Y(q0)), Z(q0))
            # Yield nested lists
            yield [X(q0), [Y(q0), Z(q0)]]
            def generator(depth):
                if depth <= 0:
                    yield CZ(q0, q1), Y(q0)
                else:
                    yield X(q0), generator(depth - 1)
                    yield Z(q0)
            # Yield nested generators
            yield generator(2)

    q0, q1 = QubitId(), QubitId()
    circuit = cirq.Circuit.from_ops(DummyGate()(q0, q1))

    opt = cirq.ExpandComposite()
    opt.optimize_circuit(circuit)
    expected = cirq.Circuit().from_ops(X(q0), Y(q0), Z(q0),  # From tuple
                                       X(q0), Y(q0), Z(q0),  # From nested lists
                                       # From nested generators
                                       X(q0), X(q0),
                                       CZ(q0, q1), Y(q0),
                                       Z(q0), Z(q0))
    assert_equal_mod_empty(expected, circuit)
def test_ignore_non_composite():
    q0, q1 = QubitId(), QubitId()
    circuit = cirq.Circuit()
    circuit.append([X(q0), Y(q1), CZ(q0, q1), Z(q0)])
    expected = circuit.copy()
    opt = cirq.ExpandComposite()
    opt.optimize_circuit(circuit)
    assert_equal_mod_empty(expected, circuit)
def test_composite_default():
    q0, q1 = QubitId(), QubitId()
    cnot = CNOT(q0, q1)
    circuit = cirq.Circuit()
    circuit.append(cnot)
    opt = cirq.ExpandComposite()
    opt.optimize_circuit(circuit)
    expected = cirq.Circuit()
    expected.append([Y(q1) ** -0.5, CZ(q0, q1), Y(q1) ** 0.5])
    assert_equal_mod_empty(expected, circuit)
Example #7
0
def test_multiple_composite_default():
    q0, q1 = QubitId(), QubitId()
    cnot = CNOT(q0, q1)
    circuit = Circuit()
    circuit.append([cnot, cnot])
    opt = ExpandComposite()
    opt.optimize_circuit(circuit)
    expected = Circuit()
    decomp = [Y(q1)**-0.5, CZ(q0, q1), Y(q1)**0.5]
    expected.append([decomp, decomp])
    assert_equal_mod_empty(expected, circuit)
def test_composite_extension_overrides():
    q0, q1 = QubitId(), QubitId()
    cnot = CNOT(q0, q1)
    circuit = cirq.Circuit()
    circuit.append(cnot)
    ext = cirq.Extensions()
    ext.add_cast(cirq.CompositeGate, cirq.CNotGate, lambda _: OtherCNot())
    opt = cirq.ExpandComposite(composite_gate_extension=ext)
    opt.optimize_circuit(circuit)
    expected = cirq.Circuit()
    expected.append([Z(q0), Y(q1) ** -0.5, CZ(q0, q1), Y(q1) ** 0.5, Z(q0)])
    assert_equal_mod_empty(expected, circuit)
def test_mix_composite_non_composite():
    q0, q1 = QubitId(), QubitId()

    actual = cirq.Circuit.from_ops(X(q0), CNOT(q0, q1), X(q1))
    opt = cirq.ExpandComposite()
    opt.optimize_circuit(actual)

    expected = cirq.Circuit.from_ops(X(q0),
                                     Y(q1) ** -0.5,
                                     CZ(q0, q1),
                                     Y(q1) ** 0.5,
                                     X(q1),
                                     strategy=cirq.InsertStrategy.NEW)
    assert_equal_mod_empty(expected, actual)
Example #10
0
def test_circuit_from_quil():
    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),
        Z(q0)**(1 / 8),
        Z(q1)**(1 / 8),
        Z(q2)**(1 / 8),
        rx(np.pi / 2)(q0),
        ry(np.pi / 2)(q1),
        rz(np.pi / 2)(q2),
        CZ(q0, q1),
        CNOT(q1, q2),
        cphase(np.pi / 2)(q0, q1),
        cphase00(np.pi / 2)(q1, q2),
        cphase01(np.pi / 2)(q0, q1),
        cphase10(np.pi / 2)(q1, q2),
        ISWAP(q0, q1),
        pswap(np.pi / 2)(q1, q2),
        SWAP(q0, q1),
        xy(np.pi / 2)(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(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()
 def generator(depth):
     if depth <= 0:
         yield CZ(q0, q1), Y(q0)
     else:
         yield X(q0), generator(depth - 1)
         yield Z(q0)
Example #12
0
moment = cirq.Moment([x, cz])
print(moment)

# define a Circuit by combining Moments togeteher
cz01 = cirq.CZ(qubits[0], qubits[1])
x2 = cirq.X(qubits[2])
cz12 = cirq.CZ(qubits[1], qubits[2])
moment0 = cirq.Moment([cz01, x2])
moment1 = cirq.Moment([cz12])
circuit = cirq.Circuit((moment0, moment1))
print(circuit)

q0, q1, q2 = [cirq.GridQubit(i, 0) for i in range(3)]

circuit = cirq.Circuit()
circuit.append([CZ(q0, q1)])
circuit.append([H(q0), H(q2)], strategy=InsertStrategy.EARLIEST)
print(circuit)

circuit = cirq.Circuit()
circuit.append([H(q0), H(q1), H(q2)], strategy=InsertStrategy.NEW)
print(circuit)
circuit = cirq.Circuit()
circuit.append([CZ(q1, q2)])
circuit.append([CZ(q1, q2)])
circuit.append([H(q0), H(q1), H(q2)], strategy=InsertStrategy.INLINE)
print(circuit)

circuit = cirq.Circuit()
circuit.append([H(q0)])
circuit.append([CZ(q1, q2), H(q0)], strategy=InsertStrategy.NEW_THEN_INLINE)
Example #13
0
print(moment)

# define circuit by combining moments
cz01 = cirq.CZ(qubits[0], qubits[1])
x2 = cirq.X(qubits[2])
cz12 = cirq.CZ(qubits[1], qubits[2])
moment0 = cirq.Moment([cz01, x2])
moment1 = cirq.Moment([cz12])
circuit = cirq.Circuit((moment0, moment1))
print(circuit)

q0, q1, q2 = [cirq.GridQubit(i, 0) for i in range(3)]

# EARLIEST insert strategy
circuit = cirq.Circuit()
circuit.append([CZ(q0, q1)])
circuit.append([H(q0), H(q2)], strategy=InsertStrategy.EARLIEST)
print(circuit, '\n\n')

# NEW insert strategy
circuit = cirq.Circuit()
operations = [H(q0), H(q1), H(q2)]
circuit.append(operations, strategy=InsertStrategy.NEW)
print(circuit, '\n\n')

# INLINE insert strategy
circuit = cirq.Circuit()
circuit.append([CZ(q1, q2)])
circuit.append([CZ(q1, q2)])
circuit.append([H(q0), H(q1), H(q2)], strategy=InsertStrategy.INLINE)
print(circuit, '\n\n')
Example #14
0
def my_layer():
    yield CZ(q0, q1)
    yield [H(q) for q in (q0, q1, q2)]
    yield [CZ(q1, q2)]
    yield [H(q0), [CZ(q1, q2)]]
Example #15
0
print(moment)

cz01 = cirq.CZ(qubits[0], qubits[1])
x2 = cirq.X(qubits[2])
cz12 = cirq.CZ(qubits[1], qubits[2])
moment0 = cirq.Moment([cz01, x2])
moment1 = cirq.Moment([cz12])
circuit = cirq.Circuit((moment0, moment1))

print('a')
print(circuit)

q0, q1, q2 = [cirq.GridQubit(i, 0) for i in range(3)]
circuit = cirq.Circuit()
circuit.append([CZ(q0, q1), H(q2)])
print(circuit)

circuit.append([H(q0), CZ(q1, q2)])
print(circuit)

circuit = cirq.Circuit()
circuit.append([CZ(q0, q1), H(q2), H(q0), CZ(q1, q2)])
print(circuit)

circuit = cirq.Circuit()
circuit.append(
    [CZ(q0, q1), H(q0), CZ(q1, q2), H(q2)], strategy=InsertStrategy.EARLIEST)
print(circuit)

circuit = cirq.Circuit()
Example #16
0
# CZ or controlled-Z  phase gate
# 2 qubit gate
import cirq
from cirq.ops import CZ, H

q0, q1, q2 = [cirq.GridQubit(i, 0) for i in range(3)]
circuit = cirq.Circuit()
circuit.append([CZ(q0, q1), H(q2)])

print(circuit)