Ejemplo n.º 1
0
def test_compile_to_syc(p):
    problem = nx.complete_graph(n=5)
    problem = random_plus_minus_1_weights(problem)
    qubits = cirq.LineQubit.range(5)
    c1 = cirq.Circuit(
        cirq.H.on_each(qubits),
        [
            [
                ProblemUnitary(problem, gamma=np.random.random()).on(*qubits),
                DriverUnitary(5, beta=np.random.random()).on(*qubits)
            ]
            for _ in range(p)
        ]
    )
    c2 = compile_problem_unitary_to_swap_network(c1)
    c3 = compile_swap_network_to_zzswap(c2)
    c4 = compile_driver_unitary_to_rx(c3)
    c5 = compile_to_syc(c4)
    validate_well_structured(c5, allow_terminal_permutations=True)

    np.testing.assert_allclose(c1.unitary(), c2.unitary())
    np.testing.assert_allclose(c1.unitary(), c3.unitary())
    np.testing.assert_allclose(c1.unitary(), c4.unitary())
    # Single qubit throws out global phase
    cirq.testing.assert_allclose_up_to_global_phase(
        c1.unitary(), c5.unitary(), atol=1e-8)
Ejemplo n.º 2
0
def test_measure_with_final_permutation(p):
    problem = nx.complete_graph(n=5)
    problem = random_plus_minus_1_weights(problem)
    qubits = cirq.LineQubit.range(5)
    c1 = cirq.Circuit(cirq.H.on_each(qubits), [[
        ProblemUnitary(problem, gamma=np.random.random()).on(*qubits),
        DriverUnitary(5, beta=np.random.random()).on(*qubits)
    ] for _ in range(p)])
    c2 = compile_problem_unitary_to_swap_network(c1)
    c3 = compile_swap_network_to_zzswap(c2)
    c4 = compile_driver_unitary_to_rx(c3)
    c5 = compile_to_syc(c4)
    validate_well_structured(c5, allow_terminal_permutations=True)
    c6, final_qubits = measure_with_final_permutation(c5, qubits)
    validate_well_structured(c6, allow_terminal_permutations=False)

    if p % 2 == 1:
        assert final_qubits == qubits[::-1]
    else:
        assert final_qubits == qubits

    permutation = []
    for q in qubits:
        permutation.append(final_qubits.index(q))
    c1_prime = (c1 +
                QuirkQubitPermutationGate('', '', permutation).on(*qubits) +
                cirq.measure(*qubits, key='z'))
    cirq.testing.assert_circuits_with_terminal_measurements_are_equivalent(
        c1_prime, c6, atol=1e-5)
Ejemplo n.º 3
0
def test_random_plus_minus_1_weights():
    g1 = nx.complete_graph(4)
    g2 = random_plus_minus_1_weights(g1, rs=np.random.RandomState(53))
    assert g1 != g2  # makes a new graph
    assert nx.is_isomorphic(g1, g2, node_match=lambda x, y: x == y)
    assert not nx.is_isomorphic(g1, g2, edge_match=lambda x, y: x == y)

    for u, v, w in g2.edges.data('weight'):
        assert 0 <= u <= 4
        assert 0 <= v <= 4
        assert w in [-1, 1]
Ejemplo n.º 4
0
def test_graph_to_serializable_edgelist():
    g = random_plus_minus_1_weights(nx.complete_graph(4),
                                    rs=np.random.RandomState(52))
    edgelist = _graph_to_serializable_edgelist(g)
    assert isinstance(edgelist, list)
    assert isinstance(edgelist[0], tuple)
    assert len(edgelist) == 4 * 3 / 2
    for n1, n2, w in edgelist:
        assert 0 <= n1 < 4
        assert 0 <= n2 < 4
        assert w in [-1, 1]
Ejemplo n.º 5
0
def test_compile_problem_unitary_to_hardware_graph():
    problem = nx.grid_2d_graph(3, 3)
    coordinates = sorted(problem.nodes)
    problem = nx.relabel_nodes(problem, {coord: i for i, coord in enumerate(coordinates)})
    problem = random_plus_minus_1_weights(problem)
    qubits = cirq.LineQubit.range(problem.number_of_nodes())

    c1 = cirq.Circuit(ProblemUnitary(problem, gamma=random.random()).on(*qubits))
    c2 = compile_problem_unitary_to_hardware_graph(c1, coordinates)
    assert c1 != c2

    np.testing.assert_allclose(c1.unitary(), c2.unitary())
Ejemplo n.º 6
0
def test_compile_problem_unitary_to_swap_network_p1():
    n = 5
    q = cirq.LineQubit.range(n)
    problem = nx.complete_graph(n=n)
    problem = random_plus_minus_1_weights(problem)

    c1 = cirq.Circuit(ProblemUnitary(problem_graph=problem, gamma=0.123).on(*q))
    c2 = compile_problem_unitary_to_swap_network(c1)
    assert c1 != c2
    assert isinstance(c2.moments[-1].operations[0].gate, QuirkQubitPermutationGate)

    u1 = c1.unitary()
    u2 = c2.unitary()
    np.testing.assert_allclose(u1, u2)
Ejemplo n.º 7
0
def test_compile_swap_network_to_zzswap():
    n = 5
    q = cirq.LineQubit.range(n)
    problem = nx.complete_graph(n=n)
    problem = random_plus_minus_1_weights(problem)

    c1 = cirq.Circuit(ProblemUnitary(problem_graph=problem, gamma=0.123).on(*q))
    c2 = compile_problem_unitary_to_swap_network(c1)
    assert c1 != c2
    c3 = compile_swap_network_to_zzswap(c2)
    assert c2 != c3

    u1 = c1.unitary()
    u3 = c3.unitary()
    np.testing.assert_allclose(u1, u3)
Ejemplo n.º 8
0
def test_swap_network_problem_unitary():
    n = 5
    q = cirq.LineQubit.range(n)
    problem = nx.complete_graph(n=n)
    problem = random_plus_minus_1_weights(problem, rs=np.random.RandomState(52))
    gamma = 0.151
    spu = SwapNetworkProblemUnitary(problem_graph=problem, gamma=gamma)
    u1 = cirq.unitary(spu)

    circuit = cirq.Circuit()
    for i1, i2, w in problem.edges.data('weight'):
        circuit.append(
            cirq.ZZPowGate(exponent=2 * gamma * w / np.pi, global_shift=-0.5).on(q[i1], q[i2]))
    circuit += QuirkQubitPermutationGate('i', 'name', list(range(n))[::-1]).on(*q)
    u2 = circuit.unitary()
    np.testing.assert_allclose(u1, u2)
Ejemplo n.º 9
0
def test_problem_unitary():
    n = 5
    q = cirq.LineQubit.range(n)
    problem = nx.complete_graph(n=n)
    problem = random_plus_minus_1_weights(problem, rs=np.random.RandomState(52))
    gamma = 0.151
    problem_unitary = ProblemUnitary(problem_graph=problem, gamma=gamma)
    u1 = cirq.unitary(problem_unitary)

    circuit = cirq.Circuit()
    for i1, i2, w in problem.edges.data('weight'):
        circuit.append(
            cirq.ZZPowGate(exponent=2 * gamma * w / np.pi, global_shift=-0.5).on(q[i1], q[i2]))
    u2 = circuit.unitary()

    np.testing.assert_allclose(u1, u2)
Ejemplo n.º 10
0
def test_compile_problem_unitary_to_zzswap():
    n = 5
    q = cirq.LineQubit.range(n)
    problem = nx.complete_graph(n=n)
    problem = random_plus_minus_1_weights(problem)

    gamma = 0.123
    circuit1 = cirq.Circuit()
    for i1, i2, w in problem.edges.data('weight'):
        circuit1.append(
            cirq.ZZPowGate(exponent=2 * gamma * w / np.pi, global_shift=-0.5).on(q[i1], q[i2]))
    u1 = circuit1.unitary()

    circuit2 = cirq.Circuit(
        compile_problem_unitary_to_zzswap(problem_graph=problem, gamma=gamma, qubits=q),
        compile_problem_unitary_to_zzswap(problem_graph=problem, gamma=0, qubits=q))
    u2 = circuit2.unitary()
    np.testing.assert_allclose(u1, u2)
Ejemplo n.º 11
0
def test_get_moment_classes():
    n = 5
    problem = nx.complete_graph(n=n)
    problem = random_plus_minus_1_weights(problem)
    qubits = cirq.LineQubit.range(n)
    p = 1
    c1 = cirq.Circuit(cirq.H.on_each(qubits), [[
        ProblemUnitary(problem, gamma=np.random.random()).on(*qubits),
        DriverUnitary(5, beta=np.random.random()).on(*qubits)
    ] for _ in range(p)])
    c2 = compile_problem_unitary_to_swap_network(c1)
    c3 = compile_swap_network_to_zzswap(c2)
    c4 = compile_driver_unitary_to_rx(c3)
    c5 = compile_to_syc(c4)
    mom_classes = get_moment_classes(c5)
    should_be = [cirq.PhasedXPowGate, cirq.ZPowGate, cg.SycamoreGate
                 ] * (n * p * 3)
    should_be += [
        cirq.PhasedXPowGate, cirq.ZPowGate, QuirkQubitPermutationGate
    ]
    assert mom_classes == should_be
Ejemplo n.º 12
0
def _random_grid_model(x, y, rs):
    connectivity_graph = nx.grid_2d_graph(x, y)
    coordinates = [(i, j) for i, j in connectivity_graph.nodes]
    problem_graph = nx.convert_node_labels_to_integers(connectivity_graph)
    problem_graph = random_plus_minus_1_weights(problem_graph, rs)
    return HardwareGridProblem(problem_graph, coordinates)
Ejemplo n.º 13
0
def test_serialized_edgelist_to_graph():
    edgelist = [(0, 1, 1.0), (0, 2, -1.0), (0, 3, 1.0), (1, 2, 1.0),
                (1, 3, 1.0), (2, 3, -1.0)]
    g = random_plus_minus_1_weights(nx.complete_graph(4),
                                    rs=np.random.RandomState(52))
    assert nx.is_isomorphic(_serialized_edgelist_to_graph(edgelist), g)
Ejemplo n.º 14
0
 def random_sk_model_with_qubit_nodes(n: int):
     graph = nx.complete_graph(n)
     graph = nx.relabel_nodes(
         graph, mapping={i: cirq.LineQubit(i)
                         for i in range(n)})
     return random_plus_minus_1_weights(graph)