Esempio n. 1
0
 def test_get_qasms_noname(self):
     """Test the get_qasms from a qprogram without names.
     """
     q_program = QuantumProgram()
     qr = q_program.create_quantum_register(size=3)
     cr = q_program.create_classical_register(size=3)
     qc1 = q_program.create_circuit(qregisters=[qr], cregisters=[cr])
     qc2 = q_program.create_circuit(qregisters=[qr], cregisters=[cr])
     qc1.h(qr[0])
     qc1.cx(qr[0], qr[1])
     qc1.cx(qr[1], qr[2])
     qc1.measure(qr[0], cr[0])
     qc1.measure(qr[1], cr[1])
     qc1.measure(qr[2], cr[2])
     qc2.h(qr)
     qc2.measure(qr[0], cr[0])
     qc2.measure(qr[1], cr[1])
     qc2.measure(qr[2], cr[2])
     results = dict(
         zip(q_program.get_circuit_names(), q_program.get_qasms()))
     qr_name_len = len(qr.name)
     cr_name_len = len(cr.name)
     self.assertEqual(len(results[qc1.name]),
                      qr_name_len * 9 + cr_name_len * 4 + 147)
     self.assertEqual(len(results[qc2.name]),
                      qr_name_len * 7 + cr_name_len * 4 + 137)
    def test_get_qasms(self):
        """Test the get_qasms.

        If all correct the qasm output for each circuit should be of a certain
        lenght

        Previusly:
            Libraries:
                from qiskit import QuantumProgram
        """
        QP_program = QuantumProgram()
        qr = QP_program.create_quantum_register("qr", 3, verbose=False)
        cr = QP_program.create_classical_register("cr", 3, verbose=False)
        qc1 = QP_program.create_circuit("qc1", [qr], [cr])
        qc2 = QP_program.create_circuit("qc2", [qr], [cr])
        qc1.h(qr[0])
        qc1.cx(qr[0], qr[1])
        qc1.cx(qr[1], qr[2])
        qc1.measure(qr[0], cr[0])
        qc1.measure(qr[1], cr[1])
        qc1.measure(qr[2], cr[2])
        qc2.h(qr)
        qc2.measure(qr[0], cr[0])
        qc2.measure(qr[1], cr[1])
        qc2.measure(qr[2], cr[2])
        result = QP_program.get_qasms(["qc1", "qc2"])
        self.assertEqual(len(result[0]), 173)
        self.assertEqual(len(result[1]), 159)
    def test_get_qasms(self):
        """Test the get_qasms.

        If all correct the qasm output for each circuit should be of a certain
        lenght

        Previusly:
            Libraries:
                from qiskit import QuantumProgram
        """
        QP_program = QuantumProgram()
        qr = QP_program.create_quantum_register("qr", 3, verbose=False)
        cr = QP_program.create_classical_register("cr", 3, verbose=False)
        qc1 = QP_program.create_circuit("qc1", [qr], [cr])
        qc2 = QP_program.create_circuit("qc2", [qr], [cr])
        qc1.h(qr[0])
        qc1.cx(qr[0], qr[1])
        qc1.cx(qr[1], qr[2])
        qc1.measure(qr[0], cr[0])
        qc1.measure(qr[1], cr[1])
        qc1.measure(qr[2], cr[2])
        qc2.h(qr)
        qc2.measure(qr[0], cr[0])
        qc2.measure(qr[1], cr[1])
        qc2.measure(qr[2], cr[2])
        result = QP_program.get_qasms(["qc1", "qc2"])
        self.assertEqual(len(result[0]), 173)
        self.assertEqual(len(result[1]), 159)
Esempio n. 4
0
def main():
    Q_program = QuantumProgram()
    Q_program.register(Qconfig.APItoken, Qconfig.config["url"])
    #Creating registers
    q = Q_program.create_quantum_register("q", 2)
    c = Q_program.create_classical_register("c", 2)
    #Quantum circuit to make shared entangled state
    superdense = Q_program.create_circuit("superdense", [q], [c])
    #Party A : Alice
    superdense.h(q[0])
    superdense.cx(q[0], q[1])
    #00:do nothing, 10:apply x, 01:apply z
    superdense.x(q[0])
    superdense.z(q[0])
    #11:apply zx
    superdense.z(q[0])
    superdense.x(q[0])
    superdense.barrier()

    #Party B : Bob
    superdense.cx(q[0], q[1])
    superdense.h(q[0])
    superdense.measure(q[0], c[0])
    superdense.measure(q[1], c[1])

    circuits = ["superdense"]
    print(Q_program.get_qasms(circuits)[0])

    backend = "ibmq_qasm_simulator"
    shots = 1024

    result = Q_program.execute(circuits, backend=backend, shots=shots, max_credits=3, timeout=240)

    print("Counts:" + str(result.get_counts("superdense")))
    plot_histogram(result.get_counts("superdense"))
Esempio n. 5
0
def main():
    # Quantum program setup
    Q_program = QuantumProgram()
    Q_program.set_api(Qconfig.APItoken, Qconfig.config["url"])

    # Creating registers
    q = Q_program.create_quantum_register("q", 2)
    c = Q_program.create_classical_register("c", 2)

    # Quantum circuit to make the shared entangled state
    superdense = Q_program.create_circuit("superdense", [q], [c])
    superdense.h(q[0])
    superdense.cx(q[0], q[1])

    # For 00, do nothing

    # For 10, apply X
    #shared.x(q[0])

    # For 01, apply Z
    #shared.z(q[0])

    # For 11, apply XZ
    superdense.z(q[0])
    superdense.x(q[0])
    superdense.barrier()

    superdense.cx(q[0], q[1])
    superdense.h(q[0])
    superdense.measure(q[0], c[0])
    superdense.measure(q[1], c[1])

    circuits = ["superdense"]
    print(Q_program.get_qasms(circuits)[0])

    backend = "local_qasm_simulator"  #'ibmqx2'  # the device to run on
    shots = 1024  # the number of shots in the experiment

    result = Q_program.execute(circuits,
                               backend=backend,
                               shots=shots,
                               max_credits=3,
                               wait=10,
                               timeout=240)

    print("Counts:" + str(result.get_counts("superdense")))

    plot_histogram(result.get_counts("superdense"))
Esempio n. 6
0
File: test.py Progetto: NickyBar/QIP
    def test_local_qasm_simulator_one_shot(self):
        QP_program = QuantumProgram(specs=QPS_SPECS)
        qc, qr, cr = QP_program.get_quantum_elements()

        qc2 = QP_program.create_circuit("qc2", ["qname"], ["cname"])
        qc3 = QP_program.create_circuit("qc3", ["qname"], ["cname"])
        qc2.h(qr[0])
        qc3.h(qr[0])
        qc2.measure(qr[0], cr[0])
        qc3.measure(qr[0], cr[0])
        circuits = ['qc2', 'qc3']

        device = 'local_qasm_simulator'  # the device to run on
        shots = 1  # the number of shots in the experiment.
        credits = 3
        coupling_map = None
        result = QP_program.execute(circuits, device, shots)
        print(QP_program.get_qasms(['qc2', 'qc3']))
        self.assertEqual(result['status'], 'COMPLETED')
 def test_get_qasms_noname(self):
     """Test the get_qasms from a qprogram without names.
     """
     q_program = QuantumProgram()
     qr = q_program.create_quantum_register(size=3)
     cr = q_program.create_classical_register(size=3)
     qc1 = q_program.create_circuit(qregisters=[qr], cregisters=[cr])
     qc2 = q_program.create_circuit(qregisters=[qr], cregisters=[cr])
     qc1.h(qr[0])
     qc1.cx(qr[0], qr[1])
     qc1.cx(qr[1], qr[2])
     qc1.measure(qr[0], cr[0])
     qc1.measure(qr[1], cr[1])
     qc1.measure(qr[2], cr[2])
     qc2.h(qr)
     qc2.measure(qr[0], cr[0])
     qc2.measure(qr[1], cr[1])
     qc2.measure(qr[2], cr[2])
     results = dict(zip(q_program.get_circuit_names(), q_program.get_qasms()))
     qr_name_len = len(qr.name)
     cr_name_len = len(cr.name)
     self.assertEqual(len(results[qc1.name]), qr_name_len * 9 + cr_name_len * 4 + 147)
     self.assertEqual(len(results[qc2.name]), qr_name_len * 7 + cr_name_len * 4 + 137)
Esempio n. 8
0
program_end = [measureZZ, measureZX, measureXX, measureXZ]

k = 0
for jj in range(30):
    theta = 2.0 * np.pi * jj / 30
    bell_middle = QuantumCircuit(q, c)
    bell_middle.ry(theta, q[0])
    for i in range(4):
        program.append('circuit' + str(k))
        Q_program.add_circuit('circuit' + str(k),
                              bell + bell_middle + program_end[i])
        k += 1

    xdata.append(theta)

Q_program.get_qasms(program[0:8])

result = Q_program.execute(program,
                           backend=backend,
                           shots=shots,
                           max_credits=3,
                           wait=10,
                           timeout=240,
                           silent=False)

CHSH_data_sim = []
k = 0
for j in range(len(xdata)):
    temp = []
    for i in range(4):
        temp.append(
Esempio n. 9
0
measureYYX.h(q3[0])
measureYYX.h(q3[1])
measureYYX.h(q3[2])
measureYYX.measure(q3[0], c3[0])
measureYYX.measure(q3[1], c3[1])
measureYYX.measure(q3[2], c3[2])

Q_program.add_circuit('ghz_measureXXX', ghz + measureXXX)
Q_program.add_circuit('ghz_measureYYX', ghz + measureYYX)
Q_program.add_circuit('ghz_measureYXY', ghz + measureYXY)
Q_program.add_circuit('ghz_measureXYY', ghz + measureXYY)

circuits = [
    'ghz_measureXXX', 'ghz_measureYYX', 'ghz_measureYXY', 'ghz_measureXYY'
]
Q_program.get_qasms(circuits)
result6 = Q_program.execute(circuits,
                            backend=backend,
                            shots=shots,
                            max_credits=5,
                            wait=10,
                            timeout=240,
                            silent=False)

temp = []
temp.append(result6.average_data('ghz_measureXXX', observable))
temp.append(result6.average_data('ghz_measureYYX', observable))
temp.append(result6.average_data('ghz_measureYXY', observable))
temp.append(result6.average_data('ghz_measureXYY', observable))
print(MerminM(temp))
def main():
    Q_program = QuantumProgram()
    Q_program.register(Qconfig.APItoken, Qconfig.config["url"])

    #Create registers
    q = Q_program.create_quantum_register('q', 3)
    c0 = Q_program.create_classical_register('c0', 1)
    c1 = Q_program.create_classical_register('c1', 1)
    c0 = Q_program.create_classical_register('c2', 1)

    #Quantum circuit to make Bell Pair
    teleport = Q_program.create_circuit('teleport', [q], [c0, c1, c2])
    teleport.h(q[1])
    teleport.cx(q[1], q[2])

    #A prepares quantum state to be teleported
    teleport.ry(np.pi / 4, q[0])
    #A applies CNOT to her two quantum states followed by H to entangle them
    teleport.cx(q[0], q[1])
    teleport.h(q[0])
    teleport.barrier()
    #A measures her two quantum states
    teleport.measure(q[0], c0[0])
    teleport.measure(q[1], c1[0])

    circuits = ['teleport']
    print(Q_program.get_qasms(circuits)[0])

    #B depending on the result applies x or z or both
    teleport.z(q[2]).c_if(c0, 1)
    teleport.x(q[2]).c_if(c1, 1)

    teleport.measure(q[2], c2[0])

    #dump asm
    circuits = ['teleport']
    print(Q_program.get_qasms(circuits)[0])

    backend = "ibmq_qasm_simulator"
    shots = 1024
    result = Q_program.execute(circuits,
                               backend=backend,
                               shots=shots,
                               max_credits=3,
                               timeout=240)

    print("Counts:" + str(result.get_counts("teleport")))

    #RESULTS
    #Alice's measurements
    data = result.get_counts("teleport")
    alice = {}
    alice['00'] = data['0 0 0'] + data['1 0 0']
    alice['10'] = data['0 1 0'] + data['1 1 0']
    alice['01'] = data['0 0 1'] + data['1 0 1']
    alice['11'] = data['0 1 1'] + data['1 1 1']
    plot_histogram(alice)

    bob = {}
    bob['0'] = data['0 0 0'] + data['0 1 0'] + data['0 0 1'] + data['0 1 1']
    bob['1'] = data['1 0 0'] + data['1 1 0'] + data['1 0 1'] + data['1 1 1']
    plot_histogram(bob)
Esempio n. 11
0
teleport.cx(q[1], q[2])

#Applying a rotation around the Y axis to prepare quantum state to be teleported
teleport.ry(np.pi / 4, q[0])

#Applying CNOT gate and Hadmard Gate
teleport.cx(q[0], q[1])
teleport.h(q[0])
teleport.barrier()

#Measuring Quantum States
teleport.measure(q[0], c0[0])
teleport.measure(q[1], c1[0])

circuits = ['teleport']
print(Q_program.get_qasms(circuits)[0])

#Applying X or Z or both, to the Quantum State
teleport.z(q[2]).c_if(c0, 1)
teleport.x(q[2]).c_if(c1, 1)

#State should be the same, Now checking through measurment
teleport.measure(q[2], c2[0])

#Excuting the changes on the Simulator
circuits = ['teleport']
print(Q_program.get_qasms(circuits)[0])

#Back-end on IBM Quantum Computer
#backend = 'ibmqx4'
backend = 'local_qasm_simulator'
Esempio n. 12
0
        }]}]
}

Q_program = QuantumProgram(specs=QPS_SPECS)
Q_program.set_api(Qconfig.APItoken, Qconfig.config["url"])

# Quantum circuits to generate bombs
circuits = ["IFM_gen"+str(i) for i in range(N)]
# NB: Can't have more than one measurement per circuit
for circuit in circuits:
    q_gen = Q_program.get_quantum_register("q_gen")
    c_gen = Q_program.get_classical_register('c_gen')
    IFM = Q_program.create_circuit(circuit, [q_gen], [c_gen])
    IFM.h(q_gen[0]) #Turn the qubit into |0> + |1>
    IFM.measure(q_gen[0], c_gen[0])
_ = Q_program.get_qasms(circuits) # Suppress the output

result = Q_program.execute(circuits, device, shots=1, max_credits=5, wait=10, timeout=240) # Note that we only want one shot
bombs = []
for circuit in circuits:
    for key in result.get_counts(circuit): # Hack, there should only be one key, since there was only one shot
        bombs.append(int(key))
#print(', '.join(('Live' if bomb else 'Dud' for bomb in bombs))) # Uncomment to print out "truth" of bombs
plot_histogram_file('bomb_generation_result.svg', Counter(('Live' if bomb else 'Dud' for bomb in bombs))) #Plotting bomb generation results

device = 'local_qasm_simulator' #Running on the simulator
circuits = ["IFM_meas"+str(i) for i in range(N)]
#Creating one measurement circuit for each bomb
for i in range(N):
    bomb = bombs[i]
    q = Q_program.get_quantum_register("q")
Esempio n. 13
0
    decodingCircuits[circuitName] = Q_program.create_circuit(
        circuitName, [qr], [cr])
    if pos == "Second":  #if pos == "First" we can directly measure
        decodingCircuits[circuitName].h(qr[0])
    decodingCircuits[circuitName].measure(qr[0], cr[0])

#combine encoding and decoding of QRACs to get a list of complete circuits
circuitNames = []
for k1 in encodingCircuits.keys():
    for k2 in decodingCircuits.keys():
        circuitNames.append(k1 + k2)
        Q_program.add_circuit(k1 + k2,
                              encodingCircuits[k1] + decodingCircuits[k2])

print("List of circuit names:", circuitNames)  #list of circuit names
Q_program.get_qasms(circuitNames)  #list qasms codes

results = Q_program.execute(circuitNames, backend=backend, shots=shots)
print("Experimental Result of Encode01DecodeFirst")
plot_histogram(results.get_counts(
    "Encode01DecodeFirst"))  #We should measure "0" with probability 0.85
print("Experimetnal Result of Encode01DecodeSecond")
plot_histogram(results.get_counts(
    "Encode01DecodeSecond"))  #We should measure "1" with probability 0.85
print("Experimental Result of Encode11DecodeFirst")
plot_histogram(results.get_counts(
    "Encode11DecodeFirst"))  #We should measure "1" with probability 0.85
print("Experimental Result of Encode11DecodeSecond")
plot_histogram(results.get_counts(
    "Encode11DecodeSecond"))  #We should measure "1" with probability 0.85
Esempio n. 14
0
def hello():
    res_string = "No data returned"
    to_run = request.get_json(force=True)  #TEST

    # Quantum program setup
    Q_program = QuantumProgram()
    Q_program.set_api(Qconfig.APItoken,
                      Qconfig.config['url'])  # set the APIToken and API url
    # Creating registers
    q = Q_program.create_quantum_register('q', 3)
    c0 = Q_program.create_classical_register('c0', 1)
    c1 = Q_program.create_classical_register('c1', 1)
    c2 = Q_program.create_classical_register('c2', 1)

    # Quantum circuit to make the shared entangled state
    teleport = Q_program.create_circuit('teleport', [q], [c0, c1, c2])
    teleport.h(q[1])
    teleport.cx(q[1], q[2])

    teleport.ry(np.pi / 4, q[0])

    teleport.cx(q[0], q[1])
    teleport.h(q[0])
    teleport.barrier()

    teleport.measure(q[0], c0[0])
    teleport.measure(q[1], c1[0])

    circuits = ['teleport']
    print(Q_program.get_qasms(circuits)[0])

    # backend = 'ibmqx2' # the backend to run on
    backend = 'local_qasm_simulator'
    shots = 1024  # the number of shots in the experiment

    result = Q_program.execute(circuits,
                               backend=backend,
                               shots=shots,
                               max_credits=3,
                               wait=10,
                               timeout=240)

    print(result.get_counts('teleport'))
    data = result.get_counts('teleport')
    return str(data)
    '''print(data);
	alice = {}
	alice['00'] = data['0 0 0'] + data['1 0 0']
	alice['10'] = data['0 1 0'] + data['1 1 0']
	alice['01'] = data['0 0 1'] + data['1 0 1']
	alice['11'] = data['0 1 1'] + data['1 1 1']
	plot_histogram(alice)

	bob = {}
	bob['0'] = data['0 0 0'] + data['0 1 0'] +  data['0 0 1'] + data['0 1 1']
	bob['1'] = data['1 0 0'] + data['1 1 0'] +  data['1 0 1'] + data['1 1 1']
	plot_histogram(bob)
	'''

    #for y in range(0, len(list)):
    #   print(list[0])

    return "Test"