forked from pierochiappina/Quantum-Computing-Projects
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmeasure_qubit.m
More file actions
24 lines (19 loc) · 725 Bytes
/
measure_qubit.m
File metadata and controls
24 lines (19 loc) · 725 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
function [state] = measure_qubit(psi)
%Takes in a qubit in the state psi, and simulates a measurement on the
%qubit. The output will be one of the basis states of the qubit, with a
%probability given by the square of the amplitude of the state in psi.
N = log2(length(psi));
r = rand; %Generates random number from 0-1
q = 0;
for i = 1:length(psi)
if r < q + (abs(psi(i)))^2
state = dec2bin(i-1, N); %Outputs state expressed in binary
break
else
q = q + (abs(psi(i)))^2;
end
end
%Ensures that the output of the function will be one of the states in psi.
%The probability that the function will output a particular state is given
%by the square of the amplitude of that state.
end