# How is the ket of a quantum state calculated in this code?

For the following code

qc = QuantumCircuit(2)
qc.h(1)
qc.cx(1,0)
ket = Statevector(qc)
ket.draw()


the output will be the following:

Statevector([0.70710678+0.j, 0.0+0.j, 0.0+0.j,0.70710678+0.j], dims=(2, 2))'

My question is how is this ket state vector calculated?

Diagram for above Quantum circuit is as follow:

In case the bra-ket notation is new to you, the calculations are fairly basic linear algebra. Your starting state is the tensor product $$(q_0)_{init} \otimes (q_1)_{init} = \vert 0\rangle\otimes\vert 0 \rangle= \begin{bmatrix} 1 \\ 0 \end{bmatrix}\otimes\begin{bmatrix} 1 \\ 0 \end{bmatrix}=\begin{bmatrix} 1 \\ 0 \\0\\0 \end{bmatrix}.$$ The first gate applies the identity operation to $$q_0$$ and the Hadamard gate to $$q_1$$, which looks like $$I\otimes H = \begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix} \otimes \tfrac{1}{\sqrt{2}}\begin{bmatrix} 1 & 1 \\ 1 & -1 \end{bmatrix} = \tfrac{1}{\sqrt{2}}\begin{bmatrix} 1 & 1 & 0 & 0 \\ 1 & -1 & 0 & 0 \\ 0 & 0 & 1 & 1 \\ 0 & 0 & 1 & -1 \end{bmatrix}.$$ The $$CNOT(\text{target}=q_0, \text{control}=q_1)$$ (note that the target and control are inverted from how you normally see the $$CNOT$$ operation) can be represented by the matrix $$CNOT_{q_0\leftarrow q_1}=\begin{bmatrix} 1&0&0&0\\0&0&0&1\\0&0&1&0\\0&1&0&0 \end{bmatrix}.$$ The matrix operations representing gates act by left multiplication, so the calculations to achieve the referenced state vector are $$\left[CNOT_{q_0\leftarrow q_1}\right]\times\left[I\otimes H\right]\times\left[(q_0)_{init} \otimes (q_1)_{init}\right],$$ which gives $$\begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0 \\ 0 & 1 & 0 & 0 \end{bmatrix} \times \tfrac{1}{\sqrt{2}} \begin{bmatrix} 1 & 1 & 0 & 0 \\ 1 & -1 & 0 & 0 \\ 0 & 0 & 1 & 1 \\ 0 & 0 & 1 & -1 \end{bmatrix} \times \begin{bmatrix} 1 \\ 0 \\ 0 \\ 0 \end{bmatrix} = \tfrac{1}{\sqrt{2}}\begin{bmatrix} 1 \\ 0 \\ 0 \\ 1 \end{bmatrix},$$ as expected.

• why identity operation is applied to q0? Commented May 18, 2022 at 15:44
• If "nothing" is applied to the qubit, it implies that the identity is applied to it. In the linear algebra sense, the $H$ operator alone acting on a single qubit is a 2x2 matrix whereas your state vector of 2 qubits is a 4x1 matrix, so you have to explicitly define all operators (including identity) so that your operators are 4x4 matrices when acting on 2 qubits Commented May 18, 2022 at 21:32

The statevector in the notation of kets are just a superposition of qubits with the registers as the index and the values as the amplitudes, e.g. for the output statevector you gave, you would have $$0.70710678|0\rangle + 0|1\rangle + 0|2\rangle + 0.70710678|3\rangle = 0.70710678|00\rangle + 0|01\rangle + 0|10\rangle + 0.70710678|11\rangle = \frac{1}{\sqrt{2}}|00\rangle + \frac{1}{\sqrt{2}}|11\rangle$$

How this is calculated is as follows:

qc = QuantumCircuit(2) means start with $$|00\rangle$$

qc.h(1) means act on the second qubit with a Hadamard gate: $$(H|0\rangle)|0\rangle = \frac{1}{\sqrt{2}}(|0\rangle+|1\rangle)|0\rangle= \frac{1}{\sqrt{2}}(|00\rangle+|10\rangle)$$

qc.cx(1,0) means act on first qubit with an $$X$$ gate controlled on the second qubit: $$CNOT\frac{1}{\sqrt{2}}(|00\rangle+|10\rangle)=\frac{1}{\sqrt{2}}(CNOT|00\rangle+CNOT|10\rangle)=\frac{1}{\sqrt{2}}(|00\rangle+|11\rangle) = \frac{1}{\sqrt{2}}|00\rangle + \frac{1}{\sqrt{2}}|11\rangle$$