Exemplo n.º 1
0
def test_get_moment_class():
    q0, q1 = cirq.LineQubit.range(2)

    # Non-hardware
    assert get_moment_class(cirq.Moment([cirq.CNOT(q0, q1)])) is None
    # Non-homogenous
    assert get_moment_class(cirq.Moment([cirq.X(q0), cirq.Z(q1)])) is None

    # Both phasedxpow
    assert get_moment_class(cirq.Moment([
        cirq.PhasedXPowGate(phase_exponent=0).on(q0),
        cirq.PhasedXPowGate(phase_exponent=0.5).on(q1)])) == cirq.PhasedXPowGate

    # Both Z
    assert get_moment_class(cirq.Moment([
        cirq.ZPowGate(exponent=0.123).on(q0),
        cirq.ZPowGate(exponent=0.5).on(q1)])) == cirq.ZPowGate

    # SYC
    assert get_moment_class(cirq.Moment([cg.SYC(q0, q1)])) == cg.SycamoreGate

    # Measurement
    assert get_moment_class(cirq.Moment([cirq.measure(q0, q1)])) == cirq.MeasurementGate

    # Permutation
    assert get_moment_class(cirq.Moment([
        QuirkQubitPermutationGate('', '', [1, 0]).on(q0, q1)])) == QuirkQubitPermutationGate
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)
Exemplo n.º 3
0
def compile_problem_unitary_to_swap_network(
        circuit: cirq.Circuit) -> cirq.Circuit:
    """Compile ProblemUnitary's in the input circuit to
    SwapNetworkProblemUnitary's with appropriate bookkeeping for permutation
    that will happen during the swap network."""
    permutation = {q: q for q in circuit.all_qubits()}

    new_moments = []
    for moment in circuit.moments:
        permuted_moment = moment.transform_qubits(lambda q: permutation[q])
        new_ops = []
        for op in permuted_moment.operations:
            if (op.gate is not None and isinstance(op.gate, ProblemUnitary)
                    and not isinstance(op.gate, SwapNetworkProblemUnitary)):
                gate = op.gate  # type: ProblemUnitary
                new_gate = SwapNetworkProblemUnitary(
                    problem_graph=gate.problem_graph, gamma=gate.gamma)
                new_op = new_gate.on(*op.qubits)
                new_ops.append(new_op)

                qubits = op.qubits
                nq = len(qubits)
                qs_to_permute = {
                    qubits[i]: qubits[nq - i - 1]
                    for i in range(nq)
                }
                permutation = {
                    q_from: qs_to_permute.get(q_to, permutation[q_to])
                    for q_from, q_to in permutation.items()
                }

            else:
                new_ops.append(op)
        new_moments.append(cirq.Moment(new_ops))

    needs_permute = sorted(q_from for q_from, q_to in permutation.items()
                           if q_from != q_to)
    # Gotta find indices
    permute_i = []
    for q_from in needs_permute:
        q_to = permutation[q_from]
        permute_i.append(needs_permute.index(q_to))

    # And tack it on
    if len(permute_i) > 0:
        new_moments.append(
            cirq.Moment([
                QuirkQubitPermutationGate('', '', permute_i).on(*needs_permute)
            ]))

    return cirq.Circuit(new_moments)
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)
Exemplo n.º 5
0
 def _decompose_(self, qubits) -> 'cirq.OP_TREE':
     yield from super()._decompose_(qubits)
     yield QuirkQubitPermutationGate('', '', list(range(len(qubits)))[::-1]).on(*qubits)