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)
Exemplo 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)
def test_compile_problem_unitary_to_swap_network_p2():
    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),
        ProblemUnitary(problem_graph=problem, gamma=0.321).on(*q),
    )
    c2 = compile_problem_unitary_to_swap_network(c1)
    assert c1 != c2
    assert not isinstance(c2.moments[-1].operations[0].gate, QuirkQubitPermutationGate)
    u1 = c1.unitary()
    u2 = c2.unitary()
    np.testing.assert_allclose(u1, u2)
Exemplo n.º 4
0
def test_compile_sk_problem_unitary_to_zzswap_2():
    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),
        DriverUnitary(num_qubits=n, beta=0.456).on(*q),
        ProblemUnitary(problem_graph=problem, gamma=0.789).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)
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())
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)
Exemplo n.º 7
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
Exemplo n.º 8
0
def get_generic_qaoa_circuit(
        problem_graph: nx.Graph,
        qubits: List[cirq.Qid],
        gammas: Sequence[float],
        betas: Sequence[float],
) -> cirq.Circuit:
    """Return a QAOA circuit with 'generic' N-body Problem and Driver unitaries

    Args:
        problem_graph: A graph with contiguous 0-indexed integer nodes and
            edges with a 'weight' attribute.
        qubits: The qubits to use in construction of the circuit.
        gammas: Gamma angles to use as parameters for problem unitaries
        betas: Beta angles to use as parameters for driver unitaries
    """
    layers = [cirq.H.on_each(*qubits)]
    assert len(gammas) == len(betas)
    for gamma, beta in zip(gammas, betas):
        layers.append(ProblemUnitary(problem_graph, gamma=gamma).on(*qubits))
        layers.append(DriverUnitary(num_qubits=len(qubits), beta=beta).on(*qubits))
    circuit = cirq.Circuit(layers)
    return circuit