In [1]:
from qiskit import *
from qiskit.tools.monitor import job_monitor
In [2]:
qr = QuantumRegister(3)
In [3]:
cr = ClassicalRegister(3)
In [4]:
circuit = QuantumCircuit(qr, cr)
In [5]:
circuit.draw(output="mpl")
Out[5]:
In [6]:
for i in range(3):
    circuit.h(i)
In [7]:
circuit.draw(output="mpl")
Out[7]:
In [8]:
circuit.measure(qr, cr)
Out[8]:
<qiskit.circuit.instructionset.InstructionSet at 0x7f360c1cfcf8>
In [9]:
circuit.draw(output="mpl")
Out[9]:
In [10]:
IBMQ.load_account()
provider = IBMQ.get_provider("ibm-q")
# Ajetaan ohjelma oikealla Ateenassa olevalla kvanttitietokoneella
computer = provider.get_backend("ibmq_valencia")
In [11]:
r = execute(circuit, backend=computer) # Testataan kvanttipiirin toiminta
In [12]:
job_monitor(r)
print(r.result().get_counts())
Job Status: job has successfully run
{'111': 96, '010': 133, '011': 119, '101': 111, '001': 133, '100': 150, '000': 156, '110': 126}
In [13]:
def noppa(circuit, computer):
    
    r = execute(circuit, backend=computer).result()
    numerot = {}
    
    max_value = 0
    max_key = 0
    isMaxUnique = True
    
    for key, value in r.get_counts().items():
        if value > max_value:
            max_value = value
            max_key = key
            isMaxUnique = True
        elif value == max_value:
            isMaxUnique = False
    
    # Jos saadaan tulokseksi 0 tai 7, heitetään uudelleen.
    # Myös jos on kaksi yhtä todennäköistä vastausta, joudutaan heittämään uudelleen
    
    if not isMaxUnique or max_key == "000" or max_key == "111":
        return noppa(circuit, simulator)
    
    return int(max_key, 2)
In [15]:
print(noppa(circuit, computer))
1
In [ ]: