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_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)
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)
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
def get_routed_sk_model_circuit( problem_graph: nx.Graph, qubits: List[cirq.Qid], gammas: Sequence[float], betas: Sequence[float], ) -> cirq.Circuit: """Get a QAOA circuit for a fully-connected problem using the linear swap network. See Also: :py:func:`get_compiled_sk_model_circuit` 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 """ circuit = get_generic_qaoa_circuit(problem_graph, qubits, gammas, betas) circuit = compile_problem_unitary_to_swap_network(circuit) circuit = compile_swap_network_to_zzswap(circuit) circuit = compile_driver_unitary_to_rx(circuit) return circuit