# How am I suposed to do a measurement in a quantum circuit?

I have a quantum circuit with 9 qubits. I have the matrix of the system. My question is how am I supposed to measure the final state of my first qubit? I know I need to apply a projection operator, but how I can write it?

import numpy as np
import matplotlib.pyplot as plt
import sys
np.set_printoptions(threshold=sys.maxsize)

qubits = [np.array([[1, 0]]) for i in range(9)]

Projector = [np.matmul(qubits[i].transpose(), qubits[i]) for i in range(9)]

State_0_projector = np.matmul(np.array([[1, 0]]).transpose(), np.array([[1, 0]]))
State_1_projector = np.matmul(np.array([[0, 1]]).transpose(), np.array([[0, 1]]))

# Gates
Id = np.identity(2)
Id9 = np.identity(512)
X = np.array([[0, 1], [1, 0]])
H = np.array([[1, 1], [1, -1]]) / np.sqrt(2)
Z = np.array([[1, 0], [0, -1]])

def kronecker(A, B, C, D, E, F, G, H, I):
matrix = np.kron(A, np.kron(B, np.kron(C, np.kron(D, np.kron(E, np.kron(F, np.kron(G, np.kron(H, I))))))))
return matrix

# Initial state
psi = kronecker(qubits[0], qubits[1], qubits[2], qubits[3], qubits[4], qubits[5], qubits[6], qubits[7], qubits[8])

#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#For simplifying consider we define MS as the final matrix of the system.

Final_State = np.matmul(MS, psi.transpose())

State_0 = kronecker(np.array([[1, 0]]), qubits[1], qubits[2], qubits[3], qubits[4], qubits[5], qubits[6], qubits[7], qubits[8])
State_1 = kronecker(np.array([[0, 1]]), qubits[1], qubits[2], qubits[3], qubits[4], qubits[5], qubits[6], qubits[7], qubits[8])

Projector_psi_0 = kronecker(Id, Id, Id, Id, Id, Id, Id, Id, State_0_projector)
Projector_psi_1 = kronecker(Id, Id, Id, Id, Id, Id, Id, Id, State_1_projector)

P = np.matmul(Projector_psi_0.transpose(), Projector_psi_0)

P_0 = (abs(np.matmul(State_0, np.matmul(P, Final_State))))**2
print(P_0)

P_1 = (abs(np.matmul(State_1, np.matmul(Projector_psi_1, Final_State))))**2
print(P_1)

$$$$
`
• What platform? Qiskit? See: qiskit.org/documentation/stubs/…
– hft
Feb 13 at 18:20
• No. I am asking using matrices. Feb 13 at 18:30
• This is a quite fundamental question and I encourage you to read up projective measurements. A projector for $|0\rangle$ is simply the outer product $|0\rangle\langle 0|$. To project 'out' individual qubits of a larger matrix you have to 'pad' this projector with identity matrices. An example for how to do a measurement that's not based on Qiskit it here. Feb 13 at 18:31
• So in the case of having 9 qubits is |000000000><000000000|? Isn't this for the whole state? I just want to the state of the first qubit. Feb 13 at 18:34
• You can do a projective measurement. If you are only measuring one qubit the result will always be either 0 or 1.
– hft
Feb 13 at 19:24

I have a quantum circuit with 9 qubits. I have the matrix of the system. My question is how am I supposed to measure the final state of my first qubit?

As an initial note, please recall that quantum computing is often described as involving two fairly distinct types of evolution: (1) "smooth" evolution due to unitary transformation; (2) "abrupt" evolution due to measurement.

If you measure a single qubit in the computational basis, you always find the answer is either 0 or 1. There is never any intermediate value measured. The measurement "collapses" the wavefunction to the projection onto the measured state.

I know I need to apply a projection operator, but how I can write it?

The exact form of the projection operator matrix will depend on your ordering conventions for qubits.

Here, I use Nielsen and Chuang's ordering convention and so I assume that the "first qubit" is the furthest "to the right" when ordering direct products.

If you only measure the first qubit of nine qubits, and you measure the value 0 (which by convention is the "top" position in the column vector notation), then the projector of interest is: $$P^{(0)}_0 = 1\otimes 1\otimes 1\otimes 1\otimes 1\otimes 1\otimes 1\otimes 1\otimes P_0\;,$$ where $$P_0$$ is: $$P_0 = \left( \begin{matrix}1 & 0 \\ 0 & 0\end{matrix} \right)$$ and where the 2-by-2 $$1$$ matrix is: $$1 = \left( \begin{matrix}1 & 0 \\ 0 & 1\end{matrix} \right)\;.$$

The end result of writing out the direct product is a $$512 \times 512$$ diagonal matrix with diagonal elements: $$1, 0, 1, 0, 1, 0, \ldots, 1, 0$$. N.b., if you use a different convention (e.g., the qiskit convention) then the end result is a diagonal matrix with the same number of 1s and 0s on the diagonal, but ordered differently (in the qiskit convention case, all the 1s would be first then followed by all the 0s).

The state of the system after the measuring 0 for the first qubit is: $$\frac{P^{(0)}_0 |\Psi\rangle}{\sqrt{\langle \Psi|{P^{(0)}_0}^\dagger P^{(0)}_0|\Psi\rangle}}\;.$$

And the probability to obtain the measurement 0 for the first qubit is: $$p(xxxxxxxx0) = \langle \Psi| {P^{(0)}_0}^\dagger P^{(0)}_0|\Psi\rangle$$

If you started your system out in some initial state $$\Psi_0$$ and evolved it via unitary gates $$U$$, then you can plug in $$|\Psi\rangle = U|\Psi_0\rangle$$.

• Could I send you my code to check where my error is? Its a small code after calculating the system matrix. Feb 13 at 21:03
• @LittleBlue If you think the code is important for the current question, you can edit your question to insert the code. You can insert source code into your post in various ways as explained here: quantumcomputing.stackexchange.com/editing-help#code
– hft
Feb 13 at 21:12
• Ok, sorry. Done. Feb 13 at 21:52
• Not a problem. It is just has a better chance of getting answered if it is complete. You may also want to add a little expository introduction to explain why you are posting the code since not everyone will read the comments to this answer. @LittleBlue
– hft
Feb 13 at 22:59
• Yea, but can you check where I did my mistake? I am losing too much time in this. Ahahahah Feb 13 at 23:16