Exemplo n.º 1
0
def test_leaves_big():
    m = circuits.DropNegligible(0.001)
    q = ops.QubitId()
    c = circuits.Circuit([circuits.Moment([ops.Z(q)**0.1])])
    assert m.optimization_at(c, 0, c.operation_at(q, 0)) is None

    d = circuits.Circuit(c.moments)
    m.optimize_circuit(d)
    assert d == c
Exemplo n.º 2
0
def test_clears_known_empties_even_at_zero_tolerance():
    m = circuits.DropNegligible(0.001)
    q = ops.QubitId()
    q2 = ops.QubitId()
    c = circuits.Circuit.from_ops(
        ops.Z(q)**0,
        ops.Y(q)**0.0000001,
        ops.X(q)**-0.0000001,
        ops.CZ(q, q2)**0)

    m.optimize_circuit(c)
    assert c == circuits.Circuit([circuits.Moment()] * 4)
Exemplo n.º 3
0
def test_clears_small():
    m = circuits.DropNegligible(0.001)
    q = ops.QubitId()
    c = circuits.Circuit([circuits.Moment([ops.Z(q)**0.000001])])

    assert (m.optimization_at(c, 0, c.operation_at(q, 0)) ==
            circuits.PointOptimizationSummary(clear_span=1,
                                              clear_qubits=[q],
                                              new_operations=[]))

    m.optimize_circuit(c)
    assert c == circuits.Circuit([circuits.Moment()])
Exemplo n.º 4
0
def assert_optimizes(before, after, optimizer=None):
    if optimizer is None:
        optimizer = MergeRotations()
    optimizer.optimize_circuit(before)

    # Ignore differences that would be caught by follow-up optimizations.
    followup_optimizations = [
        circuits.DropNegligible(),
        circuits.DropEmptyMoments()
    ]
    for post in followup_optimizations:
        post.optimize_circuit(before)
        post.optimize_circuit(after)

    if before != after:
        # coverage: ignore
        print("before:", before)
        print("after:", after)
    assert before == after
Exemplo n.º 5
0
def assert_optimizes(before, after):
    opt = MergeInteractions()
    opt.optimize_circuit(before)

    # Ignore differences that would be caught by follow-up optimizations.
    followup_optimizations = [
        MergeRotations(),
        circuits.DropNegligible(),
        circuits.DropEmptyMoments()
    ]
    for post in followup_optimizations:
        post.optimize_circuit(before)
        post.optimize_circuit(after)

    if before != after:
        # coverage: ignore
        print('ACTUAL')
        print(before)
        print('EXPECTED')
        print(after)
    assert before == after
Exemplo n.º 6
0
from cirq import circuits, ops, devices
from cirq.google import (convert_to_xmon_gates, merge_rotations,
                         merge_interactions, eject_full_w, eject_z,
                         xmon_device)

_TOLERANCE = 1e-5

_OPTIMIZERS = [
    convert_to_xmon_gates.ConvertToXmonGates(),
    merge_interactions.MergeInteractions(tolerance=_TOLERANCE,
                                         allow_partial_czs=False),
    merge_rotations.MergeRotations(tolerance=_TOLERANCE),
    eject_full_w.EjectFullW(tolerance=_TOLERANCE),
    eject_z.EjectZ(tolerance=_TOLERANCE),
    circuits.DropNegligible(tolerance=_TOLERANCE),
]


def optimized_for_xmon(
    circuit: circuits.Circuit,
    new_device: Optional[xmon_device.XmonDevice] = None,
    qubit_map: Callable[
        [ops.QubitId],
        devices.GridQubit] = lambda e: cast(devices.GridQubit, e),
) -> circuits.Circuit:
    """Optimizes a circuit with XmonDevice in mind.

    Starts by converting the circuit's operations to the xmon gate set, then
    begins merging interactions and rotations, ejecting pi-rotations and phasing
    operations, dropping unnecessary operations, and pushing operations earlier.