Пример #1
0
def test_x_decompose_with_cirq(target, controls, free):
    """Check that decomposition of multi-control ``X`` agrees with Cirq."""
    gate = gates.X(target).controlled_by(*controls)
    qibo_decomp = gate.decompose(*free, use_toffolis=False)

    # Calculate the decomposition using Cirq.
    nqubits = max((target, ) + controls + free) + 1
    qubits = [cirq.LineQubit(i) for i in range(nqubits)]
    controls = [qubits[i] for i in controls]
    free = [qubits[i] for i in free]
    cirq_decomp = cirq.decompose_multi_controlled_x(controls, qubits[target],
                                                    free)

    assert len(qibo_decomp) == len(cirq_decomp)
    for qibo_gate, cirq_gate in zip(qibo_decomp, cirq_decomp):
        assert_cirq_gates_equivalent(qibo_gate, cirq_gate)
def test_decompose_x():
    """Verifies correctness of multi-controlled X decomposition."""
    for total_qubits_count in range(1, 8):
        qubits = cirq.LineQubit.range(total_qubits_count)
        for controls_count in range(total_qubits_count):
            gates = cirq.decompose_multi_controlled_x(
                qubits[:controls_count], qubits[controls_count],
                qubits[controls_count + 1:])

            circuit1 = cirq.Circuit([cirq.I.on(q) for q in qubits])
            circuit1.append(gates)
            result_matrix = circuit1.unitary()

            circuit2 = cirq.Circuit([cirq.I.on(q) for q in qubits])
            circuit2 += cirq.ControlledGate(
                cirq.X,
                num_controls=controls_count).on(*qubits[0:controls_count + 1])
            expected_matrix = circuit2.unitary()
            assert np.allclose(expected_matrix, result_matrix)
Пример #3
0
from optimizers.lookahead_analysis import LookAheadAnalysis as ma
import cirq

qubits = [cirq.NamedQubit(str(i)) for i in range(3)]
moment = [cirq.T(i) for i in qubits]
circuit = cirq.Circuit(cirq.decompose_multi_controlled_x())
circuit.append(moment)
circuit.append(moment)
circuit.append(moment)
circuit.append(moment)

print(circuit)
result = ma(circuit)
k = result.lookahead(2, result.find_T_gates)
result.plot_data(k)
print(k)
# ma(circuit).plot_data(window_size=3)

# 0+n(2-2)+n-(3-2)+n-(4-2).. = (n+n-(p-2))(p-1)/2