Example #1
0
def run(angles, num_shots):

	# create the qubits to be used in the circuit
	a = cirq.NamedQubit("a")
	b = cirq.NamedQubit("b")
	c = cirq.NamedQubit("c")

	# store the gates to be used when creating the circuit
	x = cirq.X
	CRy = []
	NegCRy = []
	# need to store a controlled Ry gate for each angle, as well as a negative angle one
	for ang in angles:
		CRy.append(cirq.ControlledGate(RotYGate(rads=ang)))
		NegCRy.append(cirq.ControlledGate(RotYGate(rads=-ang)))
	
	# create the circuit and add H operations to it
	circuit = cirq.Circuit()
	circuit.append(H.on(a))
	circuit.append(H.on(b))

	# for every angle, add to the circuit the encoding of that angle
	for i in range(len(angles)):

		# to make sure we are transforming the correct vector, need to NOT certain qubits
		circuit.append(x.on(a))

		if(i%2 == 0):
			circuit.append(x.on(b))

		# The C^2-Ry operation
		circuit.append(CRy[i].on(a, c))
		circuit.append(CNOT.on(a, b))
		circuit.append(NegCRy[i].on(b, c))
		circuit.append(CNOT.on(a, b))
		circuit.append(CRy[i].on(b, c))

	# measure all of the qubits
	circuit.append(cirq.measure(a))
	circuit.append(cirq.measure(b))
	circuit.append(cirq.measure(c))

	simulator = cirq.google.XmonSimulator()

	# run the circuit and get measurements
	trials = simulator.run(circuit, repetitions=num_shots)

	# use the measurements to recover the angles encoding the colors
	recovered_angles = probs(trials, num_shots)

	return recovered_angles
Example #2
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()
Example #3
0
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')
import cirq
from cirq.ops import H, T, CNOT, measure
from cirq.circuits import InsertStrategy

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

circuit.append([H(q0)], strategy=InsertStrategy.NEW)
circuit.append([H(q1)], strategy=InsertStrategy.NEW)
circuit.append([H(q2)], strategy=InsertStrategy.NEW)

circuit.append([T(q0)], strategy=InsertStrategy.NEW)
circuit.append([T(q1)], strategy=InsertStrategy.NEW)
circuit.append([T(q2)], strategy=InsertStrategy.NEW)

circuit.append([CNOT(q0, q1)], strategy=InsertStrategy.NEW)

circuit.append([measure(q0)], strategy=InsertStrategy.NEW)
circuit.append([measure(q1)], strategy=InsertStrategy.NEW)
circuit.append([measure(q2)], strategy=InsertStrategy.NEW)

print(circuit)
Example #5
0
print(moment)

# define a Circuit by combining Moments togeteher
cz01 = cirq.CZ(qubits[0], qubits[1])
x2 = cirq.X(qubits[2])
cz12 = cirq.CZ(qubits[1], qubits[2])
moment0 = cirq.Moment([cz01, x2])
moment1 = cirq.Moment([cz12])
circuit = cirq.Circuit((moment0, moment1))
print(circuit)

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

circuit = cirq.Circuit()
circuit.append([CZ(q0, q1)])
circuit.append([H(q0), H(q2)], strategy=InsertStrategy.EARLIEST)
print(circuit)

circuit = cirq.Circuit()
circuit.append([H(q0), H(q1), H(q2)], strategy=InsertStrategy.NEW)
print(circuit)
circuit = cirq.Circuit()
circuit.append([CZ(q1, q2)])
circuit.append([CZ(q1, q2)])
circuit.append([H(q0), H(q1), H(q2)], strategy=InsertStrategy.INLINE)
print(circuit)

circuit = cirq.Circuit()
circuit.append([H(q0)])
circuit.append([CZ(q1, q2), H(q0)], strategy=InsertStrategy.NEW_THEN_INLINE)
print(circuit)
Example #6
0
    if "1" in counts:
        print("You win!")
    elif "0" in counts:
        print("You lose")
    else:
        print("Something weird happened")

if runMethod == "cirq":
    import cirq
    from cirq.ops import H, S, CNOT, measure

    qubits = [cirq.GridQubit(0, i) for i in range(5)]
    q2 = qubits[2]
    circuit = cirq.Circuit()

    circuit.append(H(q2))

    if humanMove == "s":
        circuit.append(S(q2))
    else:
        circuit.append(cirq.inverse(S(q2)))

    circuit.append([
        H(qubits[opponent]),
        S(q2),
        H(q2),
        CNOT(qubits[opponent], q2),
        H(q2),
        H(q2),
        measure(q2, key="m")
    ])
Example #7
0
# define circuit by combining moments
cz01 = cirq.CZ(qubits[0], qubits[1])
x2 = cirq.X(qubits[2])
cz12 = cirq.CZ(qubits[1], qubits[2])
moment0 = cirq.Moment([cz01, x2])
moment1 = cirq.Moment([cz12])
circuit = cirq.Circuit((moment0, moment1))
print(circuit)

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

# EARLIEST insert strategy
circuit = cirq.Circuit()
circuit.append([CZ(q0, q1)])
circuit.append([H(q0), H(q2)], strategy=InsertStrategy.EARLIEST)
print(circuit, '\n\n')

# NEW insert strategy
circuit = cirq.Circuit()
operations = [H(q0), H(q1), H(q2)]
circuit.append(operations, strategy=InsertStrategy.NEW)
print(circuit, '\n\n')

# INLINE insert strategy
circuit = cirq.Circuit()
circuit.append([CZ(q1, q2)])
circuit.append([CZ(q1, q2)])
circuit.append([H(q0), H(q1), H(q2)], strategy=InsertStrategy.INLINE)
print(circuit, '\n\n')
Example #8
0
def my_layer():
    yield CZ(q0, q1)
    yield [H(q) for q in (q0, q1, q2)]
    yield [CZ(q1, q2)]
    yield [H(q0), [CZ(q1, q2)]]
Example #9
0
print(moment)

cz01 = cirq.CZ(qubits[0], qubits[1])
x2 = cirq.X(qubits[2])
cz12 = cirq.CZ(qubits[1], qubits[2])
moment0 = cirq.Moment([cz01, x2])
moment1 = cirq.Moment([cz12])
circuit = cirq.Circuit((moment0, moment1))

print('a')
print(circuit)

q0, q1, q2 = [cirq.GridQubit(i, 0) for i in range(3)]
circuit = cirq.Circuit()
circuit.append([CZ(q0, q1), H(q2)])
print(circuit)

circuit.append([H(q0), CZ(q1, q2)])
print(circuit)

circuit = cirq.Circuit()
circuit.append([CZ(q0, q1), H(q2), H(q0), CZ(q1, q2)])
print(circuit)

circuit = cirq.Circuit()
circuit.append(
    [CZ(q0, q1), H(q0), CZ(q1, q2), H(q2)], strategy=InsertStrategy.EARLIEST)
print(circuit)

circuit = cirq.Circuit()
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
Example #11
0
# CZ or controlled-Z  phase gate
# 2 qubit gate
import cirq
from cirq.ops import CZ, H

q0, q1, q2 = [cirq.GridQubit(i, 0) for i in range(3)]
circuit = cirq.Circuit()
circuit.append([CZ(q0, q1), H(q2)])

print(circuit)