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
@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)
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.
Home-page: https://github.com/PennyLaneAI/pennylane
Author:
Author-email:
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)
- default.qubit.tf (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)
- braket.aws.ahs (amazon-braket-pennylane-plugin-1.20.0)
- braket.aws.qubit (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)