Pages

Saturday, January 19, 2019

Microsoft Q# Day 1: Qubit Collapsed

After going through the quantum linear algebra, I am going to see some quantum concept by using Q#.

The first one I want to prove is measurement will collapse the qubit. I will use the AssertProb to verify the value with probability. The pseudo code is listed below:

  1. use H gate to set the qubit to superposition
  2. check the probability to 1 equals 0.5 (50%)
  3. measure the qubit
  4. check the qubit equals the measured value equals to 1 (100%). The qubit collapsed. 
the code listed below:


1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
namespace Quantum.QSharpApplication2
{
    open Microsoft.Quantum.Primitive;
    open Microsoft.Quantum.Canon;
 open Microsoft.Quantum.Extensions.Convert;

    operation Operation1 () : Unit
    {
        body(...)
        {
            using (ancilla = Qubit())
   {
    H(ancilla);

    AssertProb([PauliZ], [ancilla], Zero, 0.5, "error", 1e-5);
    AssertProb([PauliZ], [ancilla], One, 0.5, "error", 1e-5);

    mutable gate = PauliZ;
    let mX = Measure([gate], [ancilla]);
    AssertProb([gate], [ancilla], mX, 1.0, "error", 1e-5);

    Message(ToStringD(mX == Zero ? 0. | 1.));

    Reset(ancilla);
   }
        }
    }
}