I am new in QC and was wondering how to prepare a Bell state of N qubits? I’m coding in python.
Hi @xnt, welcome to the forum and to the quantum computing community!
Here’s an example of how you could code this for 2 qubits in PennyLane:
import pennylane as qml from pennylane import numpy as np dev = qml.device("default.qubit", wires=2, shots=100) @qml.qnode(dev) def circuit(): qml.Hadamard(wires=0) qml.CNOT(wires=[0, 1]) return qml.sample(qml.PauliZ(0)), qml.sample(qml.PauliZ(1)) result = circuit() isBell = np.all(result == result) print(isBell)
Here we’re first importing PennyLane and Numpy from PennyLane.
Then we’re creating a device. In this case we have chosen a simulator called ‘default.qubit’ as our device. You can chose from a several different devices from different providers, however I recommend starting by using
lightning.qubit for simplicity. Within the device we specify the number of qubits, which in PennyLane are called wires. In this case we have 2 qubits (2 wires). Finally, we have specified the number of shots for this device. We need to do this since we’re going to use ‘sample’ in our measurement, but this is not necessary if you use other kinds of measurements.
The next step is to attach the quantum circuit to the device. For this we write
@qml.qnode(dev) right before the definition of our circuit. The circuit is a normal Python function. By using this decorator we have turned our quantum circuit into what we will now call a qnode.
Within our quantum circuit we can now add the gates, embeddings and other normal things you would add to a quantum circuit.
Finally, the return value of your quantum circuit must be a measurement. You can learn more about measurements here. In this case we’re sampling from wires 0 and 1, and we’re performing the measurement on the Z basis.
Once we have defined our circuit we can execute it by calling the function. In this case we call
To confirm that we have in fact generated a Bell state we can use
np.all(result == result), which will be true if all of the results are equal for the first and second qubit.
Note that if you print
result you will get -1s and 1s. A -1 is equivalent to a ‘1’ in the computational basis because it’s basically a vector going in the negative Z axis. Instead a ‘1’ in your result is equivalent to a ‘0’ in the computational basis because it means that it’s a vector measured on the positive Z axis.
If you liked this explanation I encourage you to check out this explanation on our docs which will help you learn more about quantum circuits in PennyLane, and this demo on optimizing with quantum circuits on PennyLane.
Now that you know how this works for 2 qubits you can easily make a few modifications to make a bell state for N qubits. Here we have used the broadcast template which makes it very convenient to generate patterns (in this case the CNOT).
import pennylane as qml from pennylane import numpy as np qubits = 4 dev = qml.device("default.qubit", wires=qubits, shots=100) @qml.qnode(dev) def circuit(): qml.Hadamard(wires=0) qml.broadcast(unitary=qml.CNOT, pattern="chain", wires=range(qubits)) return [qml.sample(qml.PauliZ(i)) for i in range(qubits)] result = circuit() isBell = [np.all(result[i] == result[i+1]) for i in range(qubits-1)] print(isBell)
Please let me know if this was helpful and if you have any other questions!
Thank you so much for your help on this! You explained it really well, I appreciate your help.
I’m glad you found it helpful @xnt.
Enjoy using PennyLane!