Last active
July 31, 2023 01:04
-
-
Save rish-16/6f34b7481abbe4257216cdd032cbe78b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import qiskit as qk | |
| # Load saved account from memory | |
| qk.IBMQ.load_accounts() | |
| n = 3 | |
| q = qk.QuantumRegister(n) | |
| c = qk.ClassicalRegister(n) | |
| circ = qk.QuantumCircuit(q, c) | |
| for j in range(n): | |
| circ.h(q[j]) | |
| circ.measure(q,c) | |
| print (circ) | |
| ''' | |
| The final circuit: | |
| ┌───┐┌─┐ | |
| q0_0: |0> ┤ H ├┤M├────── | |
| ├───┤└╥┘┌─┐ | |
| q0_1: |0> ┤ H ├─╫─┤M├─── | |
| ├───┤ ║ └╥┘┌─┐ | |
| q0_2: |0> ┤ H ├─╫──╫─┤M├ | |
| └───┘ ║ ║ └╥┘ | |
| c0_0: 0 ══════╩══╬══╬═ | |
| ║ ║ | |
| c0_1: 0 ═════════╩══╬═ | |
| ║ | |
| c0_2: 0 ════════════╩═ | |
| ''' | |
| backend = qk.BasicAer.get_backend('qasm_simulator') | |
| def rand_int(): | |
| new_job = qk.execute(circ, backend, shots=1) | |
| bitstring = new_job.result().get_counts() | |
| bitstring = list(bitstring.keys())[0] | |
| integer = int(bitstring, 2) | |
| return integer | |
| a = rand_int() | |
| print (a) | |
| # >>> 5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment