MATLAB代写-PHY 3690
时间:2022-02-27
UNIVERSITY OF DETROIT MERCY
PHY 3690 MODERN PHYSICS WITH DEVICE APPLICATIONS
1

Project 1: Simulate the measurement of the N‐qubit register 
Due Friday March 4 
  Wei Sha 
Measurement of N‐qubit register, measure   . 
The general ket,   , made up of 3 qubits is given by 
000 001 010 011 100 101 110 111a b c d e f g h         , or 
1 0 0 0 0 0
0 1 0 0 0 0
0 0 1 0 0 0
0 0 0 1 0 0
0 0 0 0 1 0
0 0 0 0 0 1
0 0 0 0 0 0
0 0 0 0 0 0
a b c d e f
                                                                                                                                   
0 0
0 0
0 0
0 0
0 0
0 0
1 0
0 1
a
b
c
d
g h
e
f
g
h
                                                                                    
 
where the amplitudes a, b, c,…,h are complex numbers. You can represent   in MATLAB with a column 
vector given by psi = 
a
b
c
d
e
f
g
h
             
. So psi(1) = a, psi(2) = b, psi (3) = c, etc. You are to create a function in MATLAB 
that will measure this psi.  
 
You could just print out the values of psi(1), psi(2), etc. but that would not be in accord with the rules of 
quantum mechanics—every measurement produces not    itself but one of the basis states: 
000 , 001 ,..., 111 .  
 
You measure   once and suppose the result is  000 , when you measure it again and you get  011 . The 
probability of measuring    and getting  000 as the result is  2a a a , the square of the amplitude, and 
the probability of measuring   and getting  011 is  2d d d . You never know what you are going to get! 
   
Wei Sha

2

To simulate the measurement of the ket   , you need to create a MATLAB function. Call the function 
ket_measure. The function ket_measure accepts a column vector (array) and returns an integer. The integer 
it returns must range from 1 to the length of the array. If ket_measure(psi) returns the number 1, then the 
result of the measurement is the basis state  000 . If ket_measure(psi) = 2, then the result of the 
measurement is the basis state  001 , if ket_measure(psi) = 3, then the result of the measurement is the 
basis state  010 , etc. Keep track of how many times you call ket_measure and keep track of how many 1’s 
and 2’s and 3’s, etc. are returned. In this way you can build up a statistical picture of the squares of the 
amplitudes a2,b2,c2,…,h2.  
 
For example, the line of code: 
m=ket_measure(psi); % simulates a measurement of psi and returns an integer 
measures psi and the integer m then represents which basis state the measurement returned. 
 
ket_measure(psi) 
The logic for the function is as follows. The measurement of a 3‐qubit ket, 23=8 
basis states, is equivalent to the following. Imagine you have a bar divided into 
eight segments—shown here as different colors. The total length of the bar is 
exactly 1, the length of the lowest segment (deep red) is  2a a a   and this 
corresponds to basis state  000 . The length of the top purple segment is 
2h h h   and it corresponds to basis state  111 . The measurement process is 
equivalent to dropping an arrow (shown in red) at random and seeing into 
which colored bin it falls. In this case it fell into the bin corresponding to the 
state  101 . 
 
 
 
 
 
 
 
 
 
 
   

1








Wei Sha

3

You should make sure that psi is an array with double precision complex values. Pseudocode for 
ket_measure(psi) looks something like the following. 
 
function [psi_index] = ket_measure(psi)
1. Generate a random number, R, such that 0 ≤ R ≤ 1 % new value each time it is called 
2. Initialize q to 0. % initialize it each time it is called 
3. Keep R fixed and do the following:  
Set q = a*a= IaI2. If R < q then psi_index = 1 else 
set q = q + IbI2. If R < q then psi_index = 2 else 
set q = q + IcI2. If R < q then psi_index = 3 else 
set q = q + IdI2. If R < q then psi_index = 4 else 
set q = q + IeI2. If R < q then psi_index = 5 else 
set q = q + IfI2. If R < q then psi_index = 6 else 
set q = q + IgI2. If R < q then psi_index = 7 else 
psi_index = 8 else 
end 
 
Of course the notation q = q + IbI2 means replace q with the value q + IbI2, q is being incremented. It may be 
a good practice to use a loop inside ket_measure and recognize that a = psi(1), b = psi(2), etc. Recall that we 
used a, b, c,… to represent the complex amplitudes of the basis states so the same notation is used here. 
Note that MATLAB indexes arrays beginning with 1 so if you measure state  000 you return the number 1 
(index 1 of the array), if you measure state  001 you return the number 2. The indexing means we lose a bit 
of the elegance of the notation where 000 represented the 0th or first row in the column vector. This function 
will be embedded in code where you will need to keep track of how many times a given state is returned. 
 
1.  Allocate a column vector with 2N= 8 complex entries call it   (whatever array name you choose.). 
Initialize the state   to a particular value, we’ll see in a bit. 
2. Simulated result of a measurement (discussed above) of Sz for the three qubits. The results are 
random, like tossing a coin, but must always yield one of the basis states  000 , 001 , 010 ,..., 111 . 
The results should follow the quantum mechanical Born (after Max Born) normalization rule
2 2 2 2... 1a b g h     . If result is 010 , this means the first and third qubit are in the state  0  
(spin‐up) and the second qubit is in the state  1  (spin‐down.) You should show the data in a tabular 
form in the manner of your choice. There is one possibility shown below. 
 
Repeat the measurement–step 2 above– many times to see how the results vary, it is recommended to 
measure at least 100,000 times. Each measurement is like a roll of the dice.  You need quite a few attempts 
Wei Sha

4

to be sure the dice are fair, i.e. the probability of any number occurring is 1/6. The same thing is happening 
here; you measure    and the result is one of the basis states, you need lots of measurements to see the 
probability of any particular state occurring.  
 
 
 
To make sure you have a properly working program 
try the following: 
 
Your code can be something like this: 
 
For i=1 to Num_Loops 
  Initialize the ket    to the 
appropriate value 
  Perform a simulated measurement with your ket_measure function on    
  Keep a tally of the result of measurement 
endloop 
Display results in an exciting and interesting manner 
 
a) Initialize    to 111. Measure the ket   910000 times. The result of measurement should always be 
the basis state 111. 
 
b) Initialize    to 110. Measure the ket   910000 times. The result of measurement should always be 
the basis state 110.   
c) Initialize    to the “cat” state–Schroedinger’s Cat,   1
2
000 111CAT   . 
In this state all qubits should be 0 or 1. You should measure  000  and  111  with equal frequency.  
d) Initialize    to an equal superposition of all 23 = 8 states, 
 1
8
000 001 010 011 100 101 110 111         . You should measure all states with equal 
frequency. 
 
Deliverables for Project 1: Simulate the measurement of the N‐qubit register 
1. A pdf and *.m files for the code used in the project. Note that you do not need to write code to do all 
these tasks sequentially, you can just rerun the code with the appropriate initialization. Up to you. 
2. Printouts or tables of the data for experiments a), b), c) and d) in a neat and entertaining manner. 
State  Number of occurrences 
Percent of 
occurrences 
000   10,000  15 
001   5,000  5 
010   25,000  25 
        
111   17,000  17 
Total  100,000   
Wei Sha

5

Project 2: Quantum Computations 
Due Friday March 4 
 
 Write code to run the four quantum computations shown below.  
 Explain what you expect to measure. You should have a pretty good idea what happens to each 
qubit from the first homework assignment. Work out the quantum algebra so you know the 
amplitudes for each basis state that is being measured. 
 Perform the measurements and reconcile with your expectations. Provide the data in a neat tabular 
form.  
 You must also include the MATLAB (*.m) files to show that your code works. Those files should have 
lots of comments so the reader understands what the variables and parameters are supposed to do. 
 
Gates are applied to qubits by successively operating on    from the left—the way an operator operates on 
a ket. Initialize    to the state  000 . Naturally this does not mean all the elements of the ket,   , are 
zero. The top line is for operations on qubit 1, second line for operations on qubit 2, etc. Reading along the 
line indicates how the quantum gates, operators, are applied to the initial ket. The little meter represents a 
quantum measurement. 
 
In each case your code should do something like: 
 
Generate appropriate quantum operators 
Check that they are Hermetian (you only need to do this once) 
For i=1 to Num_Loops (should be 105) 
  Initialize the ket    
  Operate on    with the appropriate operators 
  Measure    
  Keep a tally of the result of measurement 
endloop 
Display results in an exciting manner 
   
Wei Sha

6

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
To apply the Hadamard operator to the first qubit of a 3 qubit system you need the operator given by 
 1ˆ ˆ ˆ ˆH H I I   , for the second qubit you need   2ˆ ˆ ˆ ˆH I H I   , and for the third   3ˆ ˆ ˆ ˆH I I H   . Of 
course the same construction applies to the phase shift operator   3Rˆ . These are given on the last page for 
reference. 
 
Interestingly the MATLAB code for the first two operators is: 
H1=kron(kron(H,I),I);    %Hadamard operator on qubit 1 
H2=kron(kron(I,H),I);    %Hadamard operator on qubit 2 
 
Deliverables for Project 2: Simulate the measurement of the N‐qubit register 
1. A pdf and *.m files for the code used in the project. 
2. Write down and explain what you expect to measure in each of the 4 simulated experiments. That is 
work out the operator algebra. 
3. Printouts or tables of the data for experiments a), b), c) and d). 
 
 
 
 
 



H
(a) (b)


H
H
H
(c)


H H



(d)
H H 
Wei Sha

7

 
 
 
Hadamard and phase‐shift gates for your enjoyment 
 
(1)
1 0 0 0 1 0 0 0
0 1 0 0 0 1 0 0
0 0 1 0 0 0 1 0
0 0 0 1 0 0 0 11ˆ
1 0 0 0 1 0 0 02
0 1 0 0 0 1 0 0
0 0 1 0 0 0 1 0
0 0 0 1 0 0 0 1
H
              
  (1)
1 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0
0 0 1 0 0 0 0 0
0 0 0 1 0 0 0 0ˆ
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
i
i
i
i
R
e
e
e
e
 



              
 
 
(2)
1 0 1 0 0 0 0 0
0 1 0 1 0 0 0 0
1 0 1 0 0 0 0 0
0 1 0 1 0 0 0 01ˆ
0 0 0 0 1 0 1 02
0 0 0 0 0 1 0 1
0 0 0 0 1 0 1 0
0 0 0 0 0 1 0 1
H
              
  (2)
1 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0ˆ
0 0 0 0 1 0 0 0
0 0 0 0 0 1 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
i
i
i
i
e
e
R
e
e





              
 
 
(3)
1 1 0 0 0 0 0 0
1 1 0 0 0 0 0 0
0 0 1 1 0 0 0 0
0 0 1 1 0 0 0 01ˆ
0 0 0 0 1 1 0 02
0 0 0 0 1 1 0 0
0 0 0 0 0 0 1 1
0 0 0 0 0 0 1 1
H
              
  (3)
1 0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 1 0 0 0 0 0
0 0 0 0 0 0 0ˆ
0 0 0 0 1 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 1 0
0 0 0 0 0 0 0
i
i
i
i
e
e
R
e
e





              


essay、essay代写