Prepare Greenberger–Horne–Zeilinger state with Quantum Circuit
First, you have to use this package in Julia.
using YaoThen let's define the oracle, it is a function of the number of qubits. The circuit looks like this:

n = 4
circuit(n) = chain(
n,
repeat(X, [1, ]),
kron(i=>H for i in 2:n),
control([2, ], 1=>X),
control([4, ], 3=>X),
control([3, ], 1=>X),
control([4, ], 3=>X),
kron(i=>H for i in 1:n),
)circuit (generic function with 1 method)Let me explain what happens here. Firstly, we have a X gate which is applied to the first qubit. We need decide how we calculate this numerically, Yao offers serveral different approach to this. The simplest (but not the most efficient) one is to use kronecker product which will product X with I on other lines to gather an operator in the whole space and then apply it to the register. The first argument n means the number of qubits.
kron(n, 1=>X)Total: 4, DataType: Complex{Float64}
kron
└─ 1=>X gateSimilar with kron, we then need to apply some controled gates.
control(n, [2, ], 1=>X)Total: 4, DataType: Complex{Float64}
control(2)
└─ (1,)=>X gateThis means there is a X gate on the first qubit that is controled by the second qubit. In fact, you can also create a controled gate with multiple control qubits, like
control(n, [2, 3], 1=>X)Total: 4, DataType: Complex{Float64}
control(2, 3)
└─ (1,)=>X gateIn the end, we need to apply H gate to all lines, of course, you can do it by kron, but we offer something more efficient called roll, this applies a single gate each time on each qubit without calculating a new large operator, which will be extremely efficient for calculating small gates that tiles on almost every lines.
The whole circuit is a chained structure of the above blocks. And we actually store a quantum circuit in a tree structure.
circuitcircuit (generic function with 1 method)After we have an circuit, we can construct a quantum register, and input it into the oracle. You will then receive this register after processing it.
r = apply!(register(bit"0000"), circuit(4))DefaultRegister{1, Complex{Float64}}
active qubits: 4/4Let's check the output:
statevec(r)16-element Array{Complex{Float64},1}:
0.707107+0.0im
0.0+0.0im
0.0+0.0im
0.0+0.0im
0.0+0.0im
0.0+0.0im
0.0+0.0im
-2.77556e-17+0.0im
2.77556e-17+0.0im
0.0+0.0im
0.0+0.0im
0.0+0.0im
0.0+0.0im
0.0+0.0im
0.0+0.0im
-0.707107+0.0imWe have a GHZ state here, try to measure the first qubit
measure(r, 1000)
GHZ state will collapse to $|0000\rangle$ or $|1111\rangle$ due to entanglement!