# importing Qiskit
from qiskit import IBMQ, Aer
from qiskit import QuantumCircuit, execute


jokes = [
"I also have a wavefunction joke but I'm afraid that it will collapse",
"I have a unique quantum joke because I am pretty sure it cannot be cloned.",
"I have a joke about quantum physics, but nobody gets it - not even me.",
"I have a quantum computing joke, but it may have decohered.",
"I could tell an entanglement joke, but you already know the second half.",
"I have a quantum computing joke, but it may and may not be more funny than a classical computing joke.",
"I have a quantum diamond joke but it has a defect.",
"I have a quantum entanglement joke, but finding a witness to it is NP-hard.",
"I have a quantum computing opinion, but it's just a projection.",
"All quantum computing jokes are so entangling",
"I have a quantum computing joke, but it’s too nisqué.",
"I have a joke about quantum computing, but I’ll tell you when it’s less noisy.",
"I'll probably have a quantum computing joke about 30 years from now.",
"I won't tell my joke about Quantum Computing, it's a bit cold.",
"I have a joke about quantum computing, but it’s not yet error corrected.",
"I have a quantum computing joke. Once built it is going to profoundly change the way we do humor.",
"It has come to the University’s attention that people really like science jokes. We aim to form a committee in the next 2-3 months made up of selected candidates that will come up with our own, school-approved joke. Please be patient and be on the lookout for our correspondence.",
"There’s a 50/50 chance I have a quantum joke.",
"I have a quantum computing joke, but it will take you at least from 5 to 15 years to get it."
]


qubits = len(bin(len(jokes))[2:])
print("Jokes: %s, Need %s Qubits" % (len(jokes), qubits))

qc = QuantumCircuit(qubits)
qc.h(range(qubits))
# TODO: Interfere or remove additional states not in the length of 
# the solution. I.e. If 8 strings, max binary rep is 1000
qc.measure_all()

qc.draw('mpl')

# Clasically it will be easier to just repeat the simulation if we get an out of bounds result :) 
while True:
    backend = Aer.get_backend('qasm_simulator')
    job = execute(qc, backend, shots=1 )
    result = job.result().get_counts()
    joke = int(list(result)[0],2)
    if (joke - 1 < len(jokes)):
        break
    
print(jokes[joke-1])