def test_get_acquaintance_size(): qubits = cirq.LineQubit.range(8) op = OtherOperation(qubits) assert op.with_qubits(qubits) == op assert cca.get_acquaintance_size(op) == 0 for s, _ in enumerate(qubits): op = cca.acquaint(*qubits[:s + 1]) assert cca.get_acquaintance_size(op) == s + 1 part_lens = (2, 2, 2, 2) acquaintance_size = 3 gate = cca.SwapNetworkGate(part_lens, acquaintance_size) op = gate(*qubits[:sum(part_lens)]) assert cca.get_acquaintance_size(op) == 3 part_lens = (2, 2, 2, 2) acquaintance_size = 4 gate = cca.SwapNetworkGate(part_lens, acquaintance_size) op = gate(*qubits[:sum(part_lens)]) assert cca.get_acquaintance_size(op) == 0 part_lens = (2, 2, 2, 2) acquaintance_size = 1 gate = cca.SwapNetworkGate(part_lens, acquaintance_size) op = gate(*qubits[:sum(part_lens)]) assert cca.get_acquaintance_size(op) == 0 part_lens = (2, 2, 2, 2) acquaintance_size = 1 gate = cca.SwapNetworkGate(part_lens, acquaintance_size) op = gate(*qubits[:sum(part_lens)]) assert cca.get_acquaintance_size(op) == 0
def test_swap_network_gate_equality(part_len_sets): acquaintance_sizes = [None, 0, 1, 2, 3] swap_gates = [cirq.SWAP, cirq.CNOT] equals_tester = ct.EqualsTester() for args in product(part_len_sets, acquaintance_sizes, swap_gates): first_gate = cca.SwapNetworkGate(*args) second_gate = cca.SwapNetworkGate(*args) equals_tester.add_equality_group(first_gate, second_gate)
def test_swap_network_permutation(part_lens, acquaintance_size): n_qubits = sum(part_lens) gate = cca.SwapNetworkGate(part_lens, acquaintance_size) expected_permutation = {i: j for i, j in zip(range(n_qubits), reversed(range(n_qubits)))} assert gate.permutation(n_qubits) == expected_permutation
def test_swap_network_gate_permutation(part_lens, acquaintance_size): n_qubits = sum(part_lens) qubits = cirq.LineQubit.range(n_qubits) swap_network_gate = cca.SwapNetworkGate(part_lens, acquaintance_size) operations = cirq.decompose_once_with_qubits(swap_network_gate, qubits) operations = list(cirq.flatten_op_tree(operations)) mapping = {q: i for i, q in enumerate(qubits)} cca.update_mapping(mapping, operations) assert mapping == {q: i for i, q in enumerate(reversed(qubits))}
def test_acquaintance_device(): with pytest.raises(ValueError): op = cirq.X(cirq.NamedQubit('q')) cca.UnconstrainedAcquaintanceDevice.validate_operation(op) qubits = cirq.LineQubit.range(4) swap_network = cca.SwapNetworkGate((1, 2, 1)) cca.UnconstrainedAcquaintanceDevice.validate_operation( cca.acquaint(*qubits[:2])) cca.UnconstrainedAcquaintanceDevice.validate_operation( swap_network(*qubits))
def test_acquaint_part_pairs(part_lens): parts = [] n_qubits = 0 for part_len in part_lens: parts.append(tuple(range(n_qubits, n_qubits + part_len))) n_qubits += part_len qubits = cirq.LineQubit.range(n_qubits) swap_network_op = cca.SwapNetworkGate(part_lens, acquaintance_size=None)(*qubits) swap_network = cirq.Circuit(swap_network_op) initial_mapping = {q: i for i, q in enumerate(qubits)} actual_opps = cca.get_logical_acquaintance_opportunities(swap_network, initial_mapping) expected_opps = set(frozenset(s + t) for s, t in combinations(parts, 2)) assert expected_opps == actual_opps
def test_swap_network_decomposition(): qubits = cirq.LineQubit.range(8) swap_network_gate = cca.SwapNetworkGate((4, 4), 5) operations = cirq.decompose_once_with_qubits(swap_network_gate, qubits) circuit = cirq.Circuit(operations) expected_text_diagram = """ 0: ───█─────────────█─────────────╲0╱─────────────█─────────█───────0↦2─── │ │ │ │ │ │ 1: ───█─────────────█─────────────╲1╱─────────────█─────────█───────1↦3─── │ │ │ │ │ │ 2: ───█─────────────█───1↦0───────╲2╱───────1↦0───█─────────█───────2↦0─── │ │ │ │ │ │ │ │ 3: ───█───█─────────█───0↦1───█───╲3╱───█───0↦1───█─────────█───█───3↦1─── │ │ │ │ │ │ │ │ │ │ 4: ───█───█───0↦1───█─────────█───╱4╲───█─────────█───0↦1───█───█───4↦6─── │ │ │ │ │ │ │ │ 5: ───────█───1↦0─────────────█───╱5╲───█─────────────1↦0───────█───5↦7─── │ │ │ │ │ │ 6: ───────█───────────────────█───╱6╲───█───────────────────────█───6↦4─── │ │ │ │ │ │ 7: ───────█───────────────────█───╱7╲───█───────────────────────█───7↦5─── """.strip() ct.assert_has_diagram(circuit, expected_text_diagram)
def test_swap_network_decomposition(): qubits = cirq.LineQubit.range(8) swap_network_gate = cca.SwapNetworkGate((4, 4), 5) operations = cirq.decompose_once_with_qubits(swap_network_gate, qubits) circuit = cirq.Circuit.from_ops(operations) actual_text_diagram = circuit.to_text_diagram() expected_text_diagram = """ 0: ───█─────────────█─────────────╲0╱─────────────█─────────█───────0↦2─── │ │ │ │ │ │ 1: ───█─────────────█─────────────╲1╱─────────────█─────────█───────1↦3─── │ │ │ │ │ │ 2: ───█─────────────█───1↦0───────╲2╱───────1↦0───█─────────█───────2↦0─── │ │ │ │ │ │ │ │ 3: ───█───█─────────█───0↦1───█───╲3╱───█───0↦1───█─────────█───█───3↦1─── │ │ │ │ │ │ │ │ │ 4: ───█───█───0↦1───█─────────█───╱4╲───█─────────█───0↦1───█───█───0↦2─── │ │ │ │ │ │ │ │ 5: ───────█───1↦0─────────────█───╱5╲───█─────────────1↦0───────█───1↦3─── │ │ │ │ │ │ 6: ───────█───────────────────█───╱6╲───█───────────────────────█───2↦0─── │ │ │ │ │ │ 7: ───────█───────────────────█───╱7╲───█───────────────────────█───3↦1─── """.strip() assert actual_text_diagram == expected_text_diagram
def test_swap_network_gate(): qubits = tuple(cirq.NamedQubit(s) for s in alphabet) acquaintance_size = 3 n_parts = 3 part_lens = (acquaintance_size - 1, ) * n_parts n_qubits = sum(part_lens) swap_network_op = cca.SwapNetworkGate( part_lens, acquaintance_size=acquaintance_size)(*qubits[:n_qubits]) swap_network = cirq.Circuit(swap_network_op) expected_text_diagram = """ a: ───×(0,0)─── │ b: ───×(0,1)─── │ c: ───×(1,0)─── │ d: ───×(1,1)─── │ e: ───×(2,0)─── │ f: ───×(2,1)─── """.strip() ct.assert_has_diagram(swap_network, expected_text_diagram) no_decomp = lambda op: isinstance(op.gate, (cca.CircularShiftGate, cca. LinearPermutationGate)) expander = cirq.ExpandComposite(no_decomp=no_decomp) expander(swap_network) expected_text_diagram = """ a: ───█───────╲0╱───█─────────────────█───────────╲0╱───█───────0↦1─── │ │ │ │ │ │ │ b: ───█───█───╲1╱───█───█─────────────█───█───────╲1╱───█───█───1↦0─── │ │ │ │ │ │ │ │ │ │ │ c: ───█───█───╱2╲───█───█───█───╲0╱───█───█───█───╱2╲───█───█───2↦3─── │ │ │ │ │ │ │ │ │ │ d: ───────█───╱3╲───█───█───█───╲1╱───█───█───█───╱3╲───────█───3↦2─── │ │ │ │ │ │ e: ─────────────────█───────█───╱2╲───█───────█─────────────────4↦5─── │ │ │ │ f: ─────────────────█───────────╱3╲───█─────────────────────────5↦4─── """.strip() ct.assert_has_diagram(swap_network, expected_text_diagram) no_decomp = lambda op: isinstance(op.gate, cca.CircularShiftGate) expander = cirq.ExpandComposite(no_decomp=no_decomp) acquaintance_size = 3 n_parts = 6 part_lens = (1, ) * n_parts n_qubits = sum(part_lens) swap_network_op = cca.SwapNetworkGate( part_lens, acquaintance_size=acquaintance_size)(*qubits[:n_qubits]) swap_network = cirq.Circuit(swap_network_op) expander(swap_network) expected_text_diagram = """ a: ───╲0╱─────────╲0╱─────────╲0╱───────── │ │ │ b: ───╱1╲───╲0╱───╱1╲───╲0╱───╱1╲───╲0╱─── │ │ │ c: ───╲0╱───╱1╲───╲0╱───╱1╲───╲0╱───╱1╲─── │ │ │ d: ───╱1╲───╲0╱───╱1╲───╲0╱───╱1╲───╲0╱─── │ │ │ e: ───╲0╱───╱1╲───╲0╱───╱1╲───╲0╱───╱1╲─── │ │ │ f: ───╱1╲─────────╱1╲─────────╱1╲───────── """.strip() ct.assert_has_diagram(swap_network, expected_text_diagram)
def test_swap_network_repr(part_lens, acquaintance_size): gate = cca.SwapNetworkGate(part_lens, acquaintance_size) ct.assert_equivalent_repr(gate)
def test_swap_network_init_error(): with pytest.raises(ValueError): cca.SwapNetworkGate(()) with pytest.raises(ValueError): cca.SwapNetworkGate((3, ))
def test_swap_network_gate_permutation(part_lens, acquaintance_size): n_qubits = sum(part_lens) swap_network_gate = cca.SwapNetworkGate(part_lens, acquaintance_size) cca.testing.assert_permutation_decomposition_equivalence( swap_network_gate, n_qubits)
def test_swap_network_permutation_error(): gate = cca.SwapNetworkGate((1, 1)) with pytest.raises(ValueError): gate.permutation(1)