Skip to content

Instantly share code, notes, and snippets.

@benhosmer
Created April 29, 2012 09:49
Show Gist options
  • Select an option

  • Save benhosmer/2549087 to your computer and use it in GitHub Desktop.

Select an option

Save benhosmer/2549087 to your computer and use it in GitHub Desktop.
A BASH script to generate 100 random numbers using the Quantum Random Number Generator, and write them to a file.
#!/bin/bash
# This script requires the Quantum Random library https://github.com/lmacken/quantumrandom
# Use pip: pip install quantumrandom http://pypi.python.org/pypi/quantumrandom
for i in {1..100};
do qrandom --int --max 1 --min 1000 >> 100-numbers.txt;
done;
echo "Complete!";
@mrluunguyen1984-hash

Copy link
Copy Markdown

Set the number of qubits
num_qubits = 3

Initialize a quantum device

dev = qml.device("default.qubit", wires=num_qubits)

Quantum circuit to generate random numbers and calculate decimal value

@qml.qnode(dev)
def random_circuit():
for i in range(num_qubits):
qml.Hadamard(wires=i)
return [qml.expval(qml.PauliZ(wires=i)) for i in range(num_qubits)]

Execute the quantum circuit

random_numbers = random_circuit()

Convert measurement outcomes to decimal

decimal_value = sum([random_numbers[i] * 2**i for i in range(num_qubits)])

Print the random numbers and the corresponding decimal value

print("Random numbers generated by the quantum circuit:", random_numbers)
print("Decimal value:", decimal_value

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment