1
$\begingroup$
from qiskit import *
def qrng(bit_num, backend):
    bitstring = ""
    qrng_circuit = QuantumCircuit(5, 5)
    qrng_circuit.h(4)
    qrng_circuit.measure(4, 4)
    for _ in range(bit_num):
        counts = execute(qrng_circuit, backend, shots=8192).result().get_counts()
        bit = list(counts.keys())[0]  
        bitstring += bit
    return bitstring

backend = BasicAer.get_backend('qasm_simulator')

print(qrng(1024, backend))

For the above code is supposed to generate a random sequence by measurement of some specific qubits. For example if i have a quantum circuit with 10 qubits which are all put in superposition using Hadamard gate but I want that the measurement method would be random (to be able to select say 2, 3, 5, 7, 8th qubit) leading to random bitstring being generated.

$\endgroup$
2
$\begingroup$

To add some information to KAJ226 answer.

To generate random bit string having uniform distribution with quantum computer, you do not need to perform random measurement. Lets say you want to generate bit string of length $n$. Then simply put Hadamard gate on each qubit, then apply measurement and send your circuit to quantum computer with one shot as proposed by KAJ226. Repeat this $m$ times. In the end, you will have $m$ uniformly distributed random bit strings with length $n$. The reason why this works is that Hadamard gate prepares equally distributed superposition.

$\endgroup$
  • $\begingroup$ thanx. i know the method u are suggesting,however, i was looking for a way to make it more difficult for someone to know or guess the sequence.i am actually trying to compare the randomness of the hadammard method with the above suggested by @kaj226. $\endgroup$ – parth Jan 8 at 8:25
  • $\begingroup$ @parth: I think that it would be difficult to guess the algorithm behind as quantum computer is a physical random number generator with circuit cosisting of H gates. A patern in random bit strings should be similar to those generated by for example termal noise. $\endgroup$ – Martin Vesely Jan 9 at 7:28
  • $\begingroup$ Totally agree.Thanx $\endgroup$ – parth Jan 11 at 5:22
1
$\begingroup$

I am not sure exactly what you are trying to do by doing this but this does what you are looking for:

from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
from numpy import pi
from numpy.random import randint
from numpy.random import shuffle

num_qubits  = 10
qreg_q = QuantumRegister(num_qubits, 'q')
creg_c = ClassicalRegister(num_qubits, 'c')
circuit = QuantumCircuit(qreg_q, creg_c)
for i in range(num_qubits):
    circuit.h(qreg_q[i])
    
sequence = [i for i in range(num_qubits)]
shuffle(sequence)


num_measurements = 6
for i in range(num_measurements):
    circuit.measure(qreg_q[sequence[i] ], creg_c[sequence[i] ])

circuit.draw( ) 

This will create a layer of Hadamard gate on n-qubits then you can select the number of measurements you want to execute. It will then place those measurement randomly on different qubits. Thus, if you run the above code, you might generate a circuit something like:

      ┌───┐                  
 q_0: ┤ H ├──────────────────
      ├───┤            ┌─┐   
 q_1: ┤ H ├────────────┤M├───
      ├───┤            └╥┘   
 q_2: ┤ H ├─────────────╫────
      ├───┤             ║    
 q_3: ┤ H ├─────────────╫────
      ├───┤         ┌─┐ ║    
 q_4: ┤ H ├─────────┤M├─╫────
      ├───┤         └╥┘ ║    
 q_5: ┤ H ├──────────╫──╫────
      ├───┤   ┌─┐    ║  ║    
 q_6: ┤ H ├───┤M├────╫──╫────
      ├───┤   └╥┘    ║  ║ ┌─┐
 q_7: ┤ H ├────╫─────╫──╫─┤M├
      ├───┤┌─┐ ║     ║  ║ └╥┘
 q_8: ┤ H ├┤M├─╫─────╫──╫──╫─
      ├───┤└╥┘ ║ ┌─┐ ║  ║  ║ 
 q_9: ┤ H ├─╫──╫─┤M├─╫──╫──╫─
      └───┘ ║  ║ └╥┘ ║  ║  ║ 
c: 10/══════╩══╩══╩══╩══╩══╩═
            8  6  9  4  1  7 

Updated: So now, if we want to read-out the random bitstring, we can do:

backend = BasicAer.get_backend('qasm_simulator')
counts = execute(circuit, backend, shots=1).result().get_counts()
bit_strings = list(counts.keys())[0]  
print('random bit_strings:', bit_strings)

which would output:

random bit_strings: 0011010010
$\endgroup$
  • $\begingroup$ i am trying to generate a random bitstring using qubit measurement and doing that with the above code is not giving desired results. $\endgroup$ – parth Jan 7 at 9:14
  • $\begingroup$ Hi @parth, that is because I was only intended to create the circuit with random measurement chosen for you. You can perform 1 shot qasm run and readout the random bitstring. See the updated answer. $\endgroup$ – KAJ226 Jan 7 at 16:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.