def test_decompose_returns_deep_op_tree():
    class DummyGate(cirq.Gate, cirq.CompositeGate):
        def default_decompose(self, qubits):
            q0, q1 = qubits
            # Yield a tuple
            yield ((X(q0), Y(q0)), Z(q0))
            # Yield nested lists
            yield [X(q0), [Y(q0), Z(q0)]]
            def generator(depth):
                if depth <= 0:
                    yield CZ(q0, q1), Y(q0)
                else:
                    yield X(q0), generator(depth - 1)
                    yield Z(q0)
            # Yield nested generators
            yield generator(2)

    q0, q1 = QubitId(), QubitId()
    circuit = cirq.Circuit.from_ops(DummyGate()(q0, q1))

    opt = cirq.ExpandComposite()
    opt.optimize_circuit(circuit)
    expected = cirq.Circuit().from_ops(X(q0), Y(q0), Z(q0),  # From tuple
                                       X(q0), Y(q0), Z(q0),  # From nested lists
                                       # From nested generators
                                       X(q0), X(q0),
                                       CZ(q0, q1), Y(q0),
                                       Z(q0), Z(q0))
    assert_equal_mod_empty(expected, circuit)
def test_mix_composite_non_composite():
    q0, q1 = QubitId(), QubitId()

    actual = cirq.Circuit.from_ops(X(q0), CNOT(q0, q1), X(q1))
    opt = cirq.ExpandComposite()
    opt.optimize_circuit(actual)

    expected = cirq.Circuit.from_ops(X(q0),
                                     Y(q1) ** -0.5,
                                     CZ(q0, q1),
                                     Y(q1) ** 0.5,
                                     X(q1),
                                     strategy=cirq.InsertStrategy.NEW)
    assert_equal_mod_empty(expected, actual)
 def default_decompose(self, qubits):
     q0, q1 = qubits
     # Yield a tuple
     yield ((X(q0), Y(q0)), Z(q0))
     # Yield nested lists
     yield [X(q0), [Y(q0), Z(q0)]]
     def generator(depth):
         if depth <= 0:
             yield CZ(q0, q1), Y(q0)
         else:
             yield X(q0), generator(depth - 1)
             yield Z(q0)
     # Yield nested generators
     yield generator(2)
def test_ignore_non_composite():
    q0, q1 = QubitId(), QubitId()
    circuit = cirq.Circuit()
    circuit.append([X(q0), Y(q1), CZ(q0, q1), Z(q0)])
    expected = circuit.copy()
    opt = cirq.ExpandComposite()
    opt.optimize_circuit(circuit)
    assert_equal_mod_empty(expected, circuit)
def modadd(a, N, b_qubits, anc):

    for gate in psiadd(a, b_qubits, 1, [
            anc[1], anc[2]
    ]):  # Adds a to b in the fouier space conditioned on the 2 qubits in anc.
        yield gate
    for gate in psiadd(N, b_qubits,
                       -1):  # Subtracts N from b in the fouier space.
        yield gate

    for gate in reversecir(
            qft(b_qubits)
    ):  # Performs the inverse qft, applies turns anc[0] to 1 if the msf is 1, then applies the qft.
        yield gate
    yield CX(b_qubits[0], anc[0])
    for gate in qft(b_qubits):
        yield gate

    for gate in psiadd(
            N, b_qubits, 1,
        [anc[0]]):  # Adds N to b in the fouier space conditioned on anc[0].
        yield gate
    for gate in psiadd(
            a, b_qubits, -1, [anc[1], anc[2]]
    ):  # Subtracts a from b in the fouier space conditioned on the 2 qubits in anc.
        yield gate

    for gate in reversecir(
            qft(b_qubits)
    ):  # Performs the inverse qft, turns anc[0] to 0 ensuring it will end in the same state, then applies the qft.
        yield gate
    yield X(b_qubits[0])
    yield CX(b_qubits[0], anc[0])
    yield X(b_qubits[0])
    for gate in qft(b_qubits):
        yield gate

    for gate in psiadd(a, b_qubits, 1, [
            anc[1], anc[2]
    ]):  # Adds a to b in the fouier space conditioned on the 2 qubits in anc.
        yield gate
def test_decompose_returns_not_flat_op_tree():
    class DummyGate(cirq.Gate, cirq.CompositeGate):
        def default_decompose(self, qubits):
            q0, = qubits
            # Yield a tuple of gates instead of yielding a gate
            yield X(q0),

    q0 = QubitId()
    circuit = cirq.Circuit.from_ops(DummyGate()(q0))

    opt = cirq.ExpandComposite()
    opt.optimize_circuit(circuit)
    expected = cirq.Circuit().from_ops(X(q0))
    assert_equal_mod_empty(expected, circuit)
Beispiel #7
0
def test_circuit_from_quil():
    q0, q1, q2 = LineQubit.range(3)
    cirq_circuit = Circuit([
        I(q0),
        I(q1),
        I(q2),
        X(q0),
        Y(q1),
        Z(q2),
        H(q0),
        S(q1),
        T(q2),
        Z(q0)**(1 / 8),
        Z(q1)**(1 / 8),
        Z(q2)**(1 / 8),
        rx(np.pi / 2)(q0),
        ry(np.pi / 2)(q1),
        rz(np.pi / 2)(q2),
        CZ(q0, q1),
        CNOT(q1, q2),
        cphase(np.pi / 2)(q0, q1),
        cphase00(np.pi / 2)(q1, q2),
        cphase01(np.pi / 2)(q0, q1),
        cphase10(np.pi / 2)(q1, q2),
        ISWAP(q0, q1),
        pswap(np.pi / 2)(q1, q2),
        SWAP(q0, q1),
        xy(np.pi / 2)(q1, q2),
        CCNOT(q0, q1, q2),
        CSWAP(q0, q1, q2),
        MeasurementGate(1, key="ro[0]")(q0),
        MeasurementGate(1, key="ro[1]")(q1),
        MeasurementGate(1, key="ro[2]")(q2),
    ])
    # build the same Circuit, using Quil
    quil_circuit = circuit_from_quil(QUIL_PROGRAM)
    # test Circuit equivalence
    assert cirq_circuit == quil_circuit

    pyquil_circuit = Program(QUIL_PROGRAM)
    # drop declare and measures, get Program unitary
    pyquil_unitary = program_unitary(pyquil_circuit[1:-3], n_qubits=3)
    # fix qubit order convention
    cirq_circuit_swapped = Circuit(SWAP(q0, q2), cirq_circuit[:-1],
                                   SWAP(q0, q2))
    # get Circuit unitary
    cirq_unitary = cirq_circuit_swapped.unitary()
    # test unitary equivalence
    assert np.isclose(pyquil_unitary, cirq_unitary).all()
def order_find(a, N, zeros_qubits, x_qubits, anc0, m_qubits):

    m_qubits.reverse(
    )  # Reverces the order of the measurment qubits so the order is cosistant with the other programs.

    for qubit in m_qubits:  # Applies hadamards to the measurment qubits.
        yield H(qubit)

    for qubit in x_qubits:  # Sets the x qubits to 1.
        yield X(qubit)

    for i, c in enumerate(
            m_qubits
    ):  # applies sucessive multiplying gates to x_qubits controlled by the measurment qubits.
        yield c_ua(a**(2**i) % N, N, zeros_qubits, x_qubits, [anc0, c])

    for gate in reversecir(qft(
            m_qubits)):  # Applies the reverse qft on the measurment qubits.
        yield gate

    yield measure(*m_qubits, key='q')
Beispiel #9
0
 def _controlled_n_not_gate(self, action_qubit, *control_qubits):
     '''Convenience function for Controlled-N NOT gate.'''
     return self._controlled_n_unitary_gate_recursive(X._unitary_(), action_qubit, *control_qubits)
 def generator(depth):
     if depth <= 0:
         yield CZ(q0, q1), Y(q0)
     else:
         yield X(q0), generator(depth - 1)
         yield Z(q0)
 def default_decompose(self, qubits):
     q0, = qubits
     # Yield a tuple of gates instead of yielding a gate
     yield X(q0),
Beispiel #12
0
sumando_2 = input("Segundo sumando en binario(4 bits)")

n = 4

a = [cirq.GridQubit(0, i) for i in range(n)]
b = [cirq.GridQubit(1, i) for i in range(n + 1)]
c = [cirq.GridQubit(2, i) for i in range(n)]

print(qubits)

# Create a circuit
circuit = cirq.Circuit()

for i in range(n):
    if sumando_1[i] == "1":
        circuit.append(X(a[n - (i + 1)]))
for i in range(n):
    if sumando_2[i] == "1":
        circuit.append(X(b[n - (i + 1)]))

for i in range(n - 1):
    circuit.append(CCX(a[i], b[i], c[i + 1]))
    circuit.append(CX(a[i], b[i]))
    circuit.append(CCX(a[i], b[i], c[i + 1]))

circuit.append(CCX(a[n - 1], b[n - 1], b[n]))
circuit.append(CX(a[n - 1], b[n - 1]))
circuit.append(CCX(a[n - 1], b[n - 1], b[n]))

circuit.append(CX(c[n - 1], b[n - 1]))
Beispiel #13
0
def test_quil_with_defgate():
    q0 = LineQubit(0)
    cirq_circuit = Circuit([X(q0), Z(q0)])
    quil_circuit = circuit_from_quil(QUIL_PROGRAM_WITH_DEFGATE)
    assert np.isclose(quil_circuit.unitary(), cirq_circuit.unitary()).all()
Beispiel #14
0
sumando_1 = input("Primer sumando en binario (4 bits)")
sumando_2 = input("Segundo sumando en binario(4 bits)")

n = 4

a = [cirq.GridQubit(0, i) for i in range(n)]
b = [cirq.GridQubit(1, i) for i in range(n + 1)]
c = [cirq.GridQubit(2, i) for i in range(n)]

# Create a circuit
circuit = cirq.Circuit()

for i in range(n):
    if sumando_1[i] == "1":
        circuit.append(X(a[n - (i + 1)]))
for i in range(n):
    if sumando_2[i] == "1":
        circuit.append(X(b[n - (i + 1)]))

for i in range(1, n):
    circuit.append(CX(a[i], b[i]))

circuit.append(CX(a[1], c[0]))

circuit.append(CCX(a[0], b[0], c[0]))
circuit.append(CX(a[2], a[1]))

circuit.append(CCX(c[0], b[1], a[1]))
circuit.append(CX(a[3], a[2]))
def run_circuit(player, light1, light0):

    results_dict = {}

    # define qubits for circuit
    q0, q1, q2 = [cirq.GridQubit(i, 0) for i in range(3)]

    # define quantum circuit
    circuit = cirq.Circuit()

    # define quantuum simulator
    simulator = cirq.Simulator()

    # if any of the values are one add an X gate at beginning of circuit for that bit
    if player == 1:
        circuit.append(X(q2))

    if light1 == 1:
        circuit.append(X(q1))

    if light0 == 1:
        circuit.append(X(q0))

    # main circuit construction
    # H ->Hadamard gate
    # CNOT -> Feynman gate
    # X -> Pauli X gate (inverter)
    # CCX -> CCNOT gate
    circuit.append(H(q0))
    circuit.append(CNOT(q2, q1))
    circuit.append(X(q1))
    circuit.append(H(q2))
    circuit.append(CNOT(q2, q0))
    circuit.append(CCX(q2, q0, q1))
    circuit.append(X(q1))
    circuit.append(cirq.measure(q0, key='x'))
    circuit.append(cirq.measure(q1, key='y'))

    # get results from 1000 runs of circuit
    results = simulator.run(circuit, repetitions=1000)

    # gets counts for each possible output
    counts = results.multi_measurement_histogram(keys=['y', 'x'])

    # place count values from circuit simlation into dictionary
    results_dict = counter_to_dict(counts)

    # obtain 1 letter value that corresponds to 1 selected output value out of 1000 possiblities
    letter_choice = parse_counts(results_dict)

    # translate the letter value into a mood value
    choice = determine_state(letter_choice)

    # print statements that can be set with boolean
    if print_circuit == True:
        print(circuit)
    if print_counts == True:
        print(counts)
    if print_stats == True:
        get_stats()
    if print_mood == True:
        print("Robot Mood = " + str(choice))

    # return the choice of the robot
    return choice