Qauntum Random Number Generator Script help

Hello! If applicable, put your complete code example down below. Make sure that your code:

  • is 100% self-contained — someone can copy-paste exactly what is here and run it to
    reproduce the behaviour you are observing
  • includes comments
import pennylane as qml
from pennylane import numpy as np

# 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
def random_circuit():
    for i in range(num_qubits):
    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)

If you want help with diagnosing an error, please put the full error message below:

The script always returns 0 and doesn't return a random number. I am newbie and need some help. thanks.

And, finally, make sure to include the versions of your packages. Specifically, show us the output of qml.about().

>>> qml.about()
Name: PennyLane
Version: 0.31.1
Summary: PennyLane is a Python quantum machine learning library by Xanadu Inc.
License: Apache License 2.0
Location: /Users/pmantha/miniconda3/envs/python310/lib/python3.10/site-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, pennylane-lightning, requests, rustworkx, scipy, semantic-version, toml, typing-extensions
Required-by: amazon-braket-pennylane-plugin, PennyLane-Lightning

Platform info:           macOS-13.5-arm64-arm-64bit
Python version:          3.10.0
Numpy version:           1.23.5
Scipy version:           1.11.1
Installed devices:
- default.gaussian (PennyLane-0.31.1)
- default.mixed (PennyLane-0.31.1)
- default.qubit (PennyLane-0.31.1)
- default.qubit.autograd (PennyLane-0.31.1)
- default.qubit.jax (PennyLane-0.31.1)
- (PennyLane-0.31.1)
- default.qubit.torch (PennyLane-0.31.1)
- default.qutrit (PennyLane-0.31.1)
- null.qubit (PennyLane-0.31.1)
- lightning.qubit (PennyLane-Lightning-0.31.0)
- (amazon-braket-pennylane-plugin-1.20.0)
- (amazon-braket-pennylane-plugin-1.20.0)
- braket.local.ahs (amazon-braket-pennylane-plugin-1.20.0)
- braket.local.qubit (amazon-braket-pennylane-plugin-1.20.0)

Hey @QuantumMan , thanks for checking in.
I think this is actually just a math problem. :slightly_smiling_face:

Try taking just one wire, the |0⟩ state, and then acting on it with a Hadamard operator. And then finding the expectation value for the Pauli-Z matrix. This Codebook node might help you!
I think you’ll figure out what happened right away. :slight_smile: Let me know if it works or if you get stuck anywhere else!
(Also, please note that the random numbers you get from an expectation measurement like that wouldn’t come from a linearly uniform distribution.)

1 Like