Пример #1
0
def test_route_circuit_reproducible_between_runs(algo):
    seed = 23
    circuit = cirq.testing.random_circuit(6, 5, 0.5, random_state=seed)
    device_graph = ccr.get_grid_device_graph(2, 3)

    swap_network = ccr.route_circuit(circuit,
                                     device_graph,
                                     algo_name=algo,
                                     random_state=seed)
    swap_network_str = str(swap_network).lstrip('\n').rstrip()
    expected_swap_network_str = """
               ┌──┐       ┌────┐       ┌──────┐
(0, 0): ───4────Z─────4────@───────4──────────────4───
                           │
(0, 1): ───2────@─────2────┼1↦0────5────@─────────5───
                │          ││           │
(0, 2): ───5────┼─────5────┼0↦1────2────┼iSwap────2───
                │          │            ││
(1, 0): ───3────┼T────3────@───────3────┼┼────────3───
                │                       ││
(1, 1): ───1────@─────1────────────1────X┼────────1───
                                         │
(1, 2): ───0────X─────0────────────0─────iSwap────0───
               └──┘       └────┘       └──────┘
    """.lstrip('\n').rstrip()
    assert swap_network_str == expected_swap_network_str
Пример #2
0
def test_route_circuit_via_unitaries(n_moments, algo, seed, make_bad):
    circuit = cirq.testing.random_circuit(4, n_moments, 0.5, random_state=seed)
    device_graph = ccr.get_grid_device_graph(3, 2)

    swap_network = ccr.route_circuit(circuit,
                                     device_graph,
                                     algo_name=algo,
                                     random_state=seed)

    logical_qubits = sorted(circuit.all_qubits())
    if len(logical_qubits) < 2:
        return
    reverse_mapping = {l: p for p, l in swap_network.initial_mapping.items()}
    physical_qubits = [reverse_mapping[l] for l in logical_qubits]
    physical_qubits += list(set(device_graph).difference(physical_qubits))
    n_unused_qubits = len(physical_qubits) - len(logical_qubits)

    if make_bad:
        swap_network.circuit += [cirq.CNOT(*physical_qubits[:2])]
    cca.return_to_initial_mapping(swap_network.circuit)

    logical_unitary = circuit.unitary(qubit_order=logical_qubits)
    logical_unitary = np.kron(logical_unitary, np.eye(1 << n_unused_qubits))
    physical_unitary = swap_network.circuit.unitary(
        qubit_order=physical_qubits)

    assert ccr.is_valid_routing(circuit, swap_network) == (not make_bad)
    assert np.allclose(physical_unitary, logical_unitary) == (not make_bad)
Пример #3
0
def test_bad_args():
    circuit = cirq.testing.random_circuit(4, 2, 0.5, random_state=5)
    device_graph = ccr.get_grid_device_graph(3, 2)
    with pytest.raises(ValueError):
        route_circuit_greedily(circuit, device_graph, max_search_radius=0)

    with pytest.raises(ValueError):
        route_circuit_greedily(circuit, device_graph, max_num_empty_steps=0)
Пример #4
0
def test_xmon_device_to_graph():
    with cirq.testing.assert_deprecated("gridqubits_to_graph_device", deadline="v0.12"):

        class TestDevice:
            qubits = cirq.GridQubit.rect(2, 11)

        foxtail_graph = ccr.xmon_device_to_graph(TestDevice())
        two_by_eleven_grid_graph = ccr.get_grid_device_graph(2, 11)
        assert foxtail_graph.nodes == two_by_eleven_grid_graph.nodes
        assert foxtail_graph.edges() == two_by_eleven_grid_graph.edges()
Пример #5
0
def test_route_circuit_reproducible_with_seed(algo, seed):
    circuit = cirq.testing.random_circuit(8, 20, 0.5, random_state=seed)
    device_graph = ccr.get_grid_device_graph(4, 3)
    wrappers = (lambda s: s, np.random.RandomState)

    swap_networks = []
    for wrapper, _ in itertools.product(wrappers, range(3)):
        swap_network = ccr.route_circuit(circuit,
                                         device_graph,
                                         algo_name=algo,
                                         random_state=wrapper(seed))
        swap_networks.append(swap_network)

    eq = cirq.testing.equals_tester.EqualsTester()
    eq.add_equality_group(*swap_networks)
Пример #6
0
def test_initialization_reproducible_between_runs():
    seed = 45
    logical_graph = nx.erdos_renyi_graph(6, 0.5, seed=seed)
    logical_graph = nx.relabel_nodes(logical_graph, cirq.LineQubit)
    device_graph = ccr.get_grid_device_graph(2, 3)
    initial_mapping = ccr.initialization.get_initial_mapping(logical_graph, device_graph, seed)
    expected_mapping = {
        cirq.GridQubit(0, 0): cirq.LineQubit(5),
        cirq.GridQubit(0, 1): cirq.LineQubit(0),
        cirq.GridQubit(0, 2): cirq.LineQubit(2),
        cirq.GridQubit(1, 0): cirq.LineQubit(3),
        cirq.GridQubit(1, 1): cirq.LineQubit(4),
        cirq.GridQubit(1, 2): cirq.LineQubit(1),
    }
    assert initial_mapping == expected_mapping
Пример #7
0
def test_route_circuit(n_moments, algo, circuit_seed, routing_seed):
    circuit = cirq.testing.random_circuit(10,
                                          n_moments,
                                          0.5,
                                          random_state=circuit_seed)
    device_graph = ccr.get_grid_device_graph(4, 3)
    swap_network = ccr.route_circuit(circuit,
                                     device_graph,
                                     algo_name=algo,
                                     random_state=routing_seed)
    assert set(swap_network.initial_mapping).issubset(device_graph)
    assert sorted(swap_network.initial_mapping.values()) == sorted(
        circuit.all_qubits())
    assert ccr.ops_are_consistent_with_device_graph(
        swap_network.circuit.all_operations(), device_graph)
    assert ccr.is_valid_routing(circuit, swap_network)
Пример #8
0
def test_calculate_quantum_volume_result_with_device_graph():
    """Test that running the main loop routes the circuit onto the given device
       graph"""
    device_qubits = [cirq.GridQubit(i, j) for i in range(2) for j in range(3)]
    results = cirq.contrib.quantum_volume.calculate_quantum_volume(
        num_qubits=3,
        depth=3,
        num_circuits=1,
        device_or_qubits=device_qubits,
        samplers=[cirq.Simulator()],
        routing_attempts=2,
        random_state=1,
    )

    assert len(results) == 1
    assert ccr.ops_are_consistent_with_device_graph(
        results[0].compiled_circuit.all_operations(),
        ccr.get_grid_device_graph(2, 3))
Пример #9
0
def test_xmon_device_to_graph():
    foxtail_graph = ccr.xmon_device_to_graph(cirq.google.Foxtail)
    two_by_eleven_grid_graph = ccr.get_grid_device_graph(2, 11)
    assert foxtail_graph.nodes == two_by_eleven_grid_graph.nodes
    assert foxtail_graph.edges() == two_by_eleven_grid_graph.edges()
Пример #10
0
def get_seeded_initial_mapping(graph_seed, init_seed):
    logical_graph = nx.erdos_renyi_graph(10, 0.5, seed=graph_seed)
    logical_graph = nx.relabel_nodes(logical_graph, cirq.LineQubit)
    device_graph = ccr.get_grid_device_graph(4, 4)
    return ccr.initialization.get_initial_mapping(logical_graph, device_graph, init_seed)
Пример #11
0
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import numpy as np
import pytest

import cirq
import cirq.contrib.acquaintance as cca
import cirq.contrib.routing as ccr


@pytest.mark.parametrize(
    'circuit,device_graph,algo', [(cirq.testing.random_circuit(
        10, 30, 0.5), ccr.get_grid_device_graph(4, 3), algo)
                                  for algo in ccr.ROUTERS for _ in range(5)] +
    [(cirq.Circuit(), ccr.get_grid_device_graph(4, 3), 'greedy')])
def test_route_circuit(circuit, device_graph, algo):
    swap_network = ccr.route_circuit(circuit, device_graph, algo_name=algo)
    assert set(swap_network.initial_mapping).issubset(device_graph)
    assert (sorted(swap_network.initial_mapping.values()) == sorted(
        circuit.all_qubits()))
    assert ccr.ops_are_consistent_with_device_graph(
        swap_network.circuit.all_operations(), device_graph)
    assert ccr.is_valid_routing(circuit, swap_network)


@pytest.mark.parametrize(
    'circuit,device_graph,algo,make_bad', [(cirq.testing.random_circuit(
        4, 8, 0.5), ccr.get_grid_device_graph(3, 2), 'greedy', make_bad)
Пример #12
0
def main():
    circuit = cirq.Circuit()
    """ASSUMES AN EXISTING FILE OF THE NAME cirq_test_out.txt !!!!!!"""
    f = open("cirq_test_out.txt", "a")
    exTestMultiply(circuit, 11, 12)
    """choice = int(input("1. Add two numbers\n2. Multiply two numbers\n3. Multiply the first n numbers together"))
    if(choice == 1):
        testAdd(circuit)
    if(choice == 2):
        testMultiply(circuit)
    if(choice == 3):
        exampleMultiply()"""

    simulator = cirq.Simulator()
    result = simulator.run(circuit)

    #print(circuit)
    #f.write(str(circuit))
    #print(result)
    circdep = 0
    for moment in circuit:
        circdep += 1
    #print(circdep)
    #print("Now running tests: ")
    if (int(sys.argv[1]) == 3):
        width = int(sys.argv[2])
        height = int(sys.argv[3])
        depth = int(sys.argv[4])
        p_qubits = [
            ThreeDQubit(row, col, lay) for row in range(width)
            for col in range(height) for lay in range(depth)
        ]
        device_graph = nx.Graph(
            pair for pair in itertools.combinations(p_qubits, 2)
            if _my_manhattan_distance(*pair) == 1)
    if (int(sys.argv[1]) == 2):
        device_graph = ccr.get_grid_device_graph(int(sys.argv[2]),
                                                 int(sys.argv[3]))

    sn = ccr.greedy.route_circuit_greedily(
        circuit, device_graph, max_search_radius=3,
        random_state=1)  # This random seed is the reason for variation
    #print(str(sn))

    swapcount = 0
    swapdepth = 0
    for moment in sn.circuit:
        temp = swapcount
        for op in moment:
            if len(op.qubits) == 2:
                #print(op.gate)
                if op.gate == cirq.contrib.acquaintance.SwapPermutationGate():
                    swapcount += 1
        if temp != swapcount:
            swapdepth += 1
    if (int(sys.argv[1]) == 2):
        outputdimdata = [sys.argv[2], " by ", sys.argv[3], "\n"]
    if (int(sys.argv[1]) == 3):
        outputdimdata = [
            sys.argv[2], " by ", sys.argv[3], " by ", sys.argv[4], "\n"
        ]
    testoutput = [
        "SWAP count: ",
        str(swapcount), "\nSWAP depth: ",
        str(swapdepth), "\n"
    ]
    f.writelines(outputdimdata)
    f.writelines(testoutput)
    """for moment:
            for gate:
                    is swap?"""

    f.close()