Exemplo n.º 1
0
def test_get_acquaintance_size():
    qubits = cirq.LineQubit.range(5)
    op = OtherOperation(qubits)
    assert op.with_qubits(qubits) == op
    assert get_acquaintance_size(op) == 0

    for s, _ in enumerate(qubits):
        op = ACQUAINT(*qubits[:s + 1])
        assert get_acquaintance_size(op) == s + 1

    part_lens = (2, 2, 2, 2)
    acquaintance_size = 3
    gate = SwapNetworkGate(part_lens, acquaintance_size)
    op = gate(*qubits[:sum(part_lens)])
    assert get_acquaintance_size(op) == 3

    part_lens = (2, 2, 2, 2)
    acquaintance_size = 4
    gate = SwapNetworkGate(part_lens, acquaintance_size)
    op = gate(*qubits[:sum(part_lens)])
    assert get_acquaintance_size(op) == 0

    part_lens = (2, 2, 2, 2)
    acquaintance_size = 1
    gate = SwapNetworkGate(part_lens, acquaintance_size)
    op = gate(*qubits[:sum(part_lens)])
    assert get_acquaintance_size(op) == 0

    part_lens = (2, 2, 2, 2)
    acquaintance_size = 1
    gate = SwapNetworkGate(part_lens, acquaintance_size)
    op = gate(*qubits[:sum(part_lens)])
    assert get_acquaintance_size(op) == 0
Exemplo n.º 2
0
def test_get_acquaintance_size():
    with pytest.raises(TypeError):
        get_acquaintance_size(cirq.Circuit())

    with pytest.raises(TypeError):
        get_acquaintance_size(3)

    circuit = cirq.Circuit(device=UnconstrainedAcquaintanceDevice)
    get_acquaintance_size(circuit)
Exemplo n.º 3
0
 def __init__(self):
     circuits.PointOptimizer.__init__(self)
     self.no_decomp = lambda op: (not get_acquaintance_size(op) or (
         isinstance(op, ops.GateOperation) and isinstance(
             op.gate, AcquaintanceOpportunityGate)))
Exemplo n.º 4
0
 def __init__(self):
     circuits.PointOptimizer.__init__(self)
     self.no_decomp = lambda op: (not get_acquaintance_size(
         op) or ops.op_gate_of_type(op, AcquaintanceOpportunityGate))
Exemplo n.º 5
0
 def __init__(self):
     self.no_decomp = lambda op: (not get_acquaintance_size(
         op) or isinstance(op.gate, AcquaintanceOpportunityGate))
Exemplo n.º 6
0
def test_complete_acquaintance_strategy():
    qubits = [cirq.NamedQubit(s) for s in alphabet]

    with pytest.raises(ValueError):
        _ = complete_acquaintance_strategy(qubits, -1)

    empty_strategy = complete_acquaintance_strategy(qubits)
    assert empty_strategy._moments == []

    trivial_strategy = complete_acquaintance_strategy(qubits[:4], 1)
    actual_text_diagram = trivial_strategy.to_text_diagram().strip()
    expected_text_diagram = """
a: ───█───

b: ───█───

c: ───█───

d: ───█───
    """.strip()
    assert actual_text_diagram == expected_text_diagram
    assert get_acquaintance_size(trivial_strategy) == 1

    is_shift_or_lin_perm = lambda op: isinstance(op.gate, (
        CircularShiftGate, LinearPermutationGate))
    expand = cirq.ExpandComposite(no_decomp=is_shift_or_lin_perm)

    quadratic_strategy = complete_acquaintance_strategy(qubits[:8], 2)
    actual_text_diagram = quadratic_strategy.to_text_diagram().strip()
    expected_text_diagram = """
a: ───×(0,0)───
      │
b: ───×(1,0)───
      │
c: ───×(2,0)───
      │
d: ───×(3,0)───
      │
e: ───×(4,0)───
      │
f: ───×(5,0)───
      │
g: ───×(6,0)───
      │
h: ───×(7,0)───
    """.strip()
    assert actual_text_diagram == expected_text_diagram
    assert get_acquaintance_size(quadratic_strategy) == 2

    expand(quadratic_strategy)
    actual_text_diagram = quadratic_strategy.to_text_diagram(
        transpose=True).strip()
    expected_text_diagram = '\n'.join(
        ("a   b   c   d   e   f   g   h        ".strip(),
         "│   │   │   │   │   │   │   │        ".strip(),
         "█───█   █───█   █───█   █───█        ".strip(),
         "│   │   │   │   │   │   │   │        ".strip(),
         "╲0╱─╱1╲ ╲0╱─╱1╲ ╲0╱─╱1╲ ╲0╱─╱1╲      ".strip(),
         "│   │   │   │   │   │   │   │        ".strip(),
         "│   █───█   █───█   █───█   │        ".strip(),
         "│   │   │   │   │   │   │   │        ".strip(),
         "│   ╲0╱─╱1╲ ╲0╱─╱1╲ ╲0╱─╱1╲ │        ".strip(),
         "│   │   │   │   │   │   │   │        ".strip(),
         "█───█   █───█   █───█   █───█        ".strip(),
         "│   │   │   │   │   │   │   │        ".strip(),
         "╲0╱─╱1╲ ╲0╱─╱1╲ ╲0╱─╱1╲ ╲0╱─╱1╲      ".strip(),
         "│   │   │   │   │   │   │   │        ".strip(),
         "│   █───█   █───█   █───█   │        ".strip(),
         "│   │   │   │   │   │   │   │        ".strip(),
         "│   ╲0╱─╱1╲ ╲0╱─╱1╲ ╲0╱─╱1╲ │        ".strip(),
         "│   │   │   │   │   │   │   │        ".strip(),
         "█───█   █───█   █───█   █───█        ".strip(),
         "│   │   │   │   │   │   │   │        ".strip(),
         "╲0╱─╱1╲ ╲0╱─╱1╲ ╲0╱─╱1╲ ╲0╱─╱1╲      ".strip(),
         "│   │   │   │   │   │   │   │        ".strip(),
         "│   █───█   █───█   █───█   │        ".strip(),
         "│   │   │   │   │   │   │   │        ".strip(),
         "│   ╲0╱─╱1╲ ╲0╱─╱1╲ ╲0╱─╱1╲ │        ".strip(),
         "│   │   │   │   │   │   │   │        ".strip(),
         "█───█   █───█   █───█   █───█        ".strip(),
         "│   │   │   │   │   │   │   │        ".strip(),
         "╲0╱─╱1╲ ╲0╱─╱1╲ ╲0╱─╱1╲ ╲0╱─╱1╲      ".strip(),
         "│   │   │   │   │   │   │   │        ".strip(),
         "│   █───█   █───█   █───█   │        ".strip(),
         "│   │   │   │   │   │   │   │        ".strip(),
         "│   ╲0╱─╱1╲ ╲0╱─╱1╲ ╲0╱─╱1╲ │        ".strip(),
         "│   │   │   │   │   │   │   │        ".strip()))
    assert actual_text_diagram == expected_text_diagram
    assert get_acquaintance_size(quadratic_strategy) == 2

    cubic_strategy = complete_acquaintance_strategy(qubits[:4], 3)
    actual_text_diagram = cubic_strategy.to_text_diagram(
        transpose=True).strip()
    expected_text_diagram = """
a      b      c      d
│      │      │      │
×(0,0)─×(0,1)─×(1,0)─×(1,1)
│      │      │      │
╱1╲────╲0╱    ╱1╲────╲0╱
│      │      │      │
×(0,0)─×(1,0)─×(1,1)─×(2,0)
│      │      │      │
│      ╲0╱────╱1╲    │
│      │      │      │
×(0,0)─×(0,1)─×(1,0)─×(1,1)
│      │      │      │
╱1╲────╲0╱    ╱1╲────╲0╱
│      │      │      │
×(0,0)─×(1,0)─×(1,1)─×(2,0)
│      │      │      │
│      ╲0╱────╱1╲    │
│      │      │      │
    """.strip()
    assert actual_text_diagram == expected_text_diagram
    assert get_acquaintance_size(cubic_strategy) == 3