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)
```