microsoft/qdk

Public

mirrored from https://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
billt/revert-mimalloc

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

katas/content/multi_qubit_measurements/state_modification/Verification.qs

60lines · modecode

1namespace Kata.Verification {
2 open Microsoft.Quantum.Convert;
3 open Microsoft.Quantum.Diagnostics;
4 open Microsoft.Quantum.Math;
5
6 // State selection using partial measurements
7 operation StateInitialize_StateSelection(alpha : Double, qs : Qubit[]) : Unit {
8 // Prepare the state to be input to the testImplementation
9 // set the second qubit in a superposition a |0⟩ + b|1⟩
10 // with a = cos alpha, b = sin alpha
11 Ry(2.0 * alpha, qs[1]);
12
13 H(qs[0]);
14 // Apply CX gate
15 CX(qs[0], qs[1]);
16 }
17
18 // Prepare the expected state of the second qubit for the exercise.
19 operation StatePrepare_StateSelection(alpha : Double, ind : Int, q : Qubit) : Unit is Adj {
20 // set the second qubit in a superposition a|0⟩ + b|1⟩
21 // with a = cos alpha, b = sin alpha
22 Ry(2.0 * alpha, q);
23 if ind == 1 {
24 // change the state to b|0⟩ + a|1⟩
25 X(q);
26 }
27 }
28
29 @EntryPoint()
30 operation CheckSolution() : Bool {
31 use qs = Qubit[2];
32 for i in 0 .. 5 {
33 let alpha = (PI() * IntAsDouble(i)) / 5.0;
34 let params = $"a = {Cos(alpha)}, b = {Sin(alpha)}";
35
36 for ind in 0 .. 1 {
37 // Prepare the state to be input to the testImplementation
38 StateInitialize_StateSelection(alpha, qs);
39
40 // operate testImplementation
41 Kata.StateSelection(qs, ind);
42 // reset the first qubit, since its state does not matter
43 Reset(qs[0]);
44
45 // apply adjoint reference operation and check that the result is correct
46 Adjoint StatePrepare_StateSelection(alpha, ind, qs[1]);
47
48 if not CheckAllZero(qs) {
49 ResetAll(qs);
50 Message("Incorrect.");
51 Message($"The state of the second qubit for {params}, ind = {ind} does not match expectation.");
52 return false;
53 }
54 }
55 }
56 Message("Correct!");
57 true
58 }
59
60}
61