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 #4
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)
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)
Example #8
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_decompose_returns_not_flat_op_tree():
    class DummyGate(cirq.Gate, cirq.CompositeGate):
        def default_decompose(self, qubits):
            q0, = qubits
            # Yield a tuple of gates instead of yielding a gate
            yield X(q0),

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

    opt = cirq.ExpandComposite()
    opt.optimize_circuit(circuit)
    expected = cirq.Circuit().from_ops(X(q0))
    assert_equal_mod_empty(expected, circuit)