I’m thirsty for simulate n-qubie GHZ state in pennylane and I wanna ask how to simulate it in pennylane.
Hey @DonaldTrump! Welcome to the forum and love the enthusiasm
I highly recommend that you check out some of our introductory content, of which we have many different offerings depending on how you like to learn :
- Codebook: This is our free-to-access textbook that also has code exercises to help you learn PennyLane alongside traditional quantum computing, information, etc. Check it out here: https://codebook.xanadu.ai/
- Demos: We have a ton of demos on our website that are really really great
. Check them out here: Getting Started | PennyLane Demos. Might be a good idea to start with this one: Basic tutorial: qubit rotation | PennyLane Demos
- Videos: We recently launched a dedicated PennyLane YouTube channel and we have a beginner video about how PennyLane works here: https://youtu.be/uCm027_jvZ0
To answer your question, though, there are a few ways you can prep a GHZ state in PennyLane:
- Gates: The GHZ state can be made with a Hadamard gate and a couple CNOTs
To do this in PennyLane, you just place the gates in a QNode and off you go!
import pennylane as qml
dev = qml.device("default.qubit")
@qml.qnode(dev)
def ghz():
qml.Hadamard(0)
qml.CNOT(wires=[0, 1])
qml.CNOT(wires=[1, 2])
return qml.state()
ghz()
tensor([0.70710678+0.j, 0. +0.j, 0. +0.j, 0. +0.j,
0. +0.j, 0. +0.j, 0. +0.j, 0.70710678+0.j], requires_grad=True)
- “Direct” state prep: You can directly encode the state into the circuit like this:
import pennylane.numpy as np
@qml.qnode(dev)
def ghz():
qml.StatePrep(np.array([1/np.sqrt(2), 0, 0, 0, 0, 0, 0, 1/np.sqrt(2)]), wires=range(3))
return qml.state()
ghz()
tensor([0.70710678+0.j, 0. +0.j, 0. +0.j, 0. +0.j,
0. +0.j, 0. +0.j, 0. +0.j, 0.70710678+0.j], requires_grad=True)
Anyway, if this is the start of your PennyLane journey, we’re glad to have you on board! Feel free to post any questions here that you may have along the way
My pleasure! Glad I could help