Skip to content
This repository was archived by the owner on Aug 21, 2024. It is now read-only.

Commit bbc8750

Browse files
authored
Joint Measurements kata (#35)
1 parent be1422a commit bbc8750

File tree

12 files changed

+655
-7
lines changed

12 files changed

+655
-7
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
// See http://go.microsoft.com/fwlink/?LinkId=827846
3+
// for the documentation about the extensions.json format
4+
"recommendations": [
5+
"quantum.quantum-devkit-vscode"
6+
]
7+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"csharp.suppressDotnetRestoreNotification": true
3+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
// See https://go.microsoft.com/fwlink/?LinkId=733558
3+
// for the documentation about the tasks.json format
4+
"version": "2.0.0",
5+
"tasks": [
6+
{
7+
"label": "build",
8+
"command": "dotnet",
9+
"args": [
10+
"build"
11+
],
12+
"type": "process",
13+
"group": "build",
14+
"presentation": {
15+
"reveal": "silent"
16+
},
17+
"problemMatcher": "$msCompile"
18+
},
19+
{
20+
"label": "test",
21+
"command": "dotnet",
22+
"args": [
23+
"test"
24+
],
25+
"type": "process",
26+
"group": "test",
27+
"presentation": {
28+
"echo": true,
29+
"reveal": "always",
30+
"focus": false,
31+
"panel": "shared"
32+
},
33+
"problemMatcher": "$msCompile"
34+
}
35+
]
36+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>netcoreapp2.0</TargetFramework>
4+
<PlatformTarget>x64</PlatformTarget>
5+
<IsPackable>false</IsPackable>
6+
<RootNamespace>Quantum.Kata.JointMeasurements</RootNamespace>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.Quantum.Canon" Version="0.2.1809.701-preview" />
11+
<PackageReference Include="Microsoft.Quantum.Development.Kit" Version="0.2.1809.701-preview" />
12+
<PackageReference Include="Microsoft.Quantum.Xunit" Version="0.2.1809.701-preview" />
13+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
14+
<PackageReference Include="xunit" Version="2.3.1" />
15+
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
16+
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
17+
</ItemGroup>
18+
19+
<ItemGroup>
20+
<None Include="README.md" />
21+
</ItemGroup>
22+
</Project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.27130.2036
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JointMeasurements", "JointMeasurements.csproj", "{F7A0175F-4217-4343-8A3C-EA68FAAB6B7B}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{F7A0175F-4217-4343-8A3C-EA68FAAB6B7B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{F7A0175F-4217-4343-8A3C-EA68FAAB6B7B}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{F7A0175F-4217-4343-8A3C-EA68FAAB6B7B}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{F7A0175F-4217-4343-8A3C-EA68FAAB6B7B}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {4E22BDB7-FE55-4D1C-98FA-BD7612C8525D}
24+
EndGlobalSection
25+
EndGlobal

JointMeasurements/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Welcome!
2+
3+
The joint measurements kata covers the usage of joint measurements, also known as parity measurements, -
4+
measurements involving multiple qubits.
5+
6+
In Q# they are implemented as the [Measure](https://docs.microsoft.com/en-us/qsharp/api/prelude/microsoft.quantum.primitive.measure) operation.
7+
8+
* You can read more about measurements of multi-qubit Pauli operators in the [Q# documentation](https://docs.microsoft.com/en-us/quantum/quantum-concepts-7-paulimeasurements).
9+
* A general-case implementation of CNOT gate via joint measurements is described in [this paper](https://arxiv.org/pdf/1201.5734.pdf).
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
//////////////////////////////////////////////////////////////////////
5+
// This file contains reference solutions to all tasks.
6+
// The tasks themselves can be found in Tasks.qs file.
7+
// We recommend that you try to solve the tasks yourself first,
8+
// but feel free to look up the solution if you get stuck.
9+
//////////////////////////////////////////////////////////////////////
10+
11+
namespace Quantum.Kata.JointMeasurements
12+
{
13+
open Microsoft.Quantum.Primitive;
14+
open Microsoft.Quantum.Canon;
15+
open Microsoft.Quantum.Extensions.Convert;
16+
open Microsoft.Quantum.Extensions.Math;
17+
18+
// Task 1. Single-qubit measurement
19+
operation SingleQubitMeasurement_Reference (qs : Qubit[]) : Int
20+
{
21+
body
22+
{
23+
// Hint: use two single-qubit measurements
24+
if (M(qs[0]) == M(qs[1])) {
25+
return 0;
26+
} else {
27+
return 1;
28+
}
29+
}
30+
}
31+
32+
// Task 2. Parity measurement
33+
operation ParityMeasurement_Reference (qs : Qubit[]) : Int
34+
{
35+
body
36+
{
37+
if (Measure([PauliZ; PauliZ], qs) == Zero) {
38+
return 0;
39+
} else {
40+
return 1;
41+
}
42+
}
43+
}
44+
45+
// Task 3. |0000⟩ + |1111⟩ or |0011⟩ + |1100⟩ ?
46+
operation GHZOrGHZWithX_Reference (qs : Qubit[]) : Int
47+
{
48+
body
49+
{
50+
if (Measure([PauliZ; PauliZ], qs[1..2]) == Zero) {
51+
return 0;
52+
} else {
53+
return 1;
54+
}
55+
}
56+
}
57+
58+
// Task 4. |0..0⟩ + |1..1⟩ or W state ?
59+
operation GHZOrWState_Reference (qs : Qubit[]) : Int
60+
{
61+
body
62+
{
63+
if (MeasureAllZ(qs) == Zero) {
64+
return 0;
65+
} else {
66+
return 1;
67+
}
68+
}
69+
}
70+
71+
// Task 5. Parity measurement in different basis
72+
operation DifferentBasis_Reference (qs : Qubit[]) : Int
73+
{
74+
body
75+
{
76+
// The first state is a superposition of |++⟩ and |--⟩,
77+
// the second one - of |+-⟩ and |-+⟩
78+
if (Measure([PauliX; PauliX], qs) == Zero) {
79+
return 0;
80+
} else {
81+
return 1;
82+
}
83+
}
84+
}
85+
86+
// Task 6. Controlled X gate with |0⟩ target
87+
operation ControlledX_Reference (qs : Qubit[]) : ()
88+
{
89+
body
90+
{
91+
H(qs[1]);
92+
if (Measure([PauliZ; PauliZ], qs) == One) {
93+
X(qs[1]);
94+
}
95+
}
96+
}
97+
98+
// Task 7*. Controlled X gate with arbitrary target
99+
operation ControlledX_General_Reference (qs : Qubit[]) : ()
100+
{
101+
body
102+
{
103+
// This implementation follows the description at https://arxiv.org/pdf/1201.5734.pdf.
104+
// Note the parity notation used in the table of fixups in the paper
105+
// differs from the notation used in Q#.
106+
using (ans = Qubit[1]) {
107+
let c = qs[0];
108+
let a = ans[0];
109+
let t = qs[1];
110+
H(a);
111+
let p1 = MeasureAllZ([c; a]);
112+
H(a);
113+
H(t);
114+
let p2 = MeasureAllZ([a; t]);
115+
H(a);
116+
H(t);
117+
let m = M(a);
118+
// apply fixups
119+
if (p2 == One) {
120+
Z(c);
121+
}
122+
if (p1 != m) {
123+
X(t);
124+
}
125+
// reset ancilla qubit
126+
if (m == One) {
127+
X(a);
128+
}
129+
}
130+
}
131+
adjoint self;
132+
}
133+
}

JointMeasurements/Tasks.qs

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
namespace Quantum.Kata.JointMeasurements
5+
{
6+
open Microsoft.Quantum.Primitive;
7+
open Microsoft.Quantum.Canon;
8+
open Microsoft.Quantum.Extensions.Convert;
9+
open Microsoft.Quantum.Extensions.Math;
10+
11+
//////////////////////////////////////////////////////////////////
12+
// Welcome!
13+
//////////////////////////////////////////////////////////////////
14+
15+
// "Joint Measurements" quantum kata is a series of exercises designed
16+
// to get you familiar with programming in Q#.
17+
// It covers the joint parity measurements and using them for distinguishing quantum states
18+
// or for performing multi-qubit gates.
19+
//
20+
// Each task is wrapped in one operation preceded by the description of the task.
21+
// Each task (except tasks in which you have to write a test) has a unit test associated with it,
22+
// which initially fails. Your goal is to fill in the blank (marked with // ... comment)
23+
// with some Q# code to make the failing test pass.
24+
//
25+
// The tasks are given in approximate order of increasing difficulty; harder ones are marked with asterisks.
26+
27+
28+
// Task 1. Single-qubit measurement
29+
// Input: Two qubits (stored in an array) which are guaranteed to be
30+
// either in superposition of states |00⟩ and |11⟩
31+
// or in superposition of states |01⟩ and |10⟩.
32+
// Output: 0 if qubits were in the first superposition,
33+
// 1 if they were in the second superposition.
34+
// The state of the qubits at the end of the operation does not matter.
35+
operation SingleQubitMeasurement (qs : Qubit[]) : Int
36+
{
37+
body
38+
{
39+
// Hint: Use two single-qubit measurements.
40+
// ...
41+
return -1;
42+
}
43+
}
44+
45+
// Task 2. Parity measurement
46+
// Input: Two qubits (stored in an array) which are guaranteed to be
47+
// either in superposition of states |00⟩ and |11⟩
48+
// or in superposition of states |01⟩ and |10⟩.
49+
// Output: 0 if qubits were in the first superposition,
50+
// 1 if they were in the second superposition.
51+
// The state of the qubits at the end of the operation should be the same as the starting state.
52+
operation ParityMeasurement (qs : Qubit[]) : Int
53+
{
54+
body
55+
{
56+
// ...
57+
return -1;
58+
}
59+
}
60+
61+
// Task 3. |0000⟩ + |1111⟩ or |0011⟩ + |1100⟩ ?
62+
// Input: Four qubits (stored in an array) which are guaranteed to be
63+
// either in superposition of states |0000⟩ and |1111⟩
64+
// or in superposition of states |0011⟩ and |1100⟩.
65+
// Output: 0 if qubits were in the first superposition,
66+
// 1 if they were in the second superposition.
67+
// The state of the qubits at the end of the operation should be the same as the starting state.
68+
operation GHZOrGHZWithX (qs : Qubit[]) : Int
69+
{
70+
body
71+
{
72+
// ...
73+
return -1;
74+
}
75+
}
76+
77+
// Task 4. |0..0⟩ + |1..1⟩ or W state ?
78+
// Input: An even number of qubits (stored in an array) which are guaranteed to be
79+
// either in superposition of states |0..0⟩ and |1..1⟩
80+
// or in W state ( https://en.wikipedia.org/wiki/W_state ).
81+
// Output: 0 if qubits were in W state,
82+
// 1 if they were in the second superposition.
83+
// The state of the qubits at the end of the operation should be the same as the starting state.
84+
operation GHZOrWState (qs : Qubit[]) : Int
85+
{
86+
body
87+
{
88+
// ...
89+
return -1;
90+
}
91+
}
92+
93+
// Task 5. Parity measurement in different basis
94+
// Input: Two qubits (stored in an array) which are guaranteed to be
95+
// either in superposition α|00⟩ + β|01⟩ + β|10⟩ + α|11⟩
96+
// or in superposition α|00⟩ - β|01⟩ + β|10⟩ - α|11⟩.
97+
// Output: 0 if qubits were in the first superposition,
98+
// 1 if they were in the second superposition.
99+
// The state of the qubits at the end of the operation should be the same as the starting state.
100+
operation DifferentBasis (qs : Qubit[]) : Int
101+
{
102+
body
103+
{
104+
// ...
105+
return -1;
106+
}
107+
}
108+
109+
// Task 6. Controlled X gate with |0⟩ target
110+
// Input: Two unentangled qubits (stored in an array of length 2).
111+
// The first qubit will be in state |ψ⟩ = α |0⟩ + β |1⟩, the second - in state |0⟩
112+
// (this can be written as two-qubit state (α|0⟩ + β|1⟩) ⊕ |0⟩).
113+
// Goal: Change the two-qubit state to α |00⟩ + β |11⟩ using only single-qubit gates and joint measurements.
114+
// Do not use two-qubit gates.
115+
// You do not need to allocate extra qubits.
116+
operation ControlledX (qs : Qubit[]) : ()
117+
{
118+
body
119+
{
120+
// ...
121+
}
122+
}
123+
124+
// Task 7*. Controlled X gate with arbitrary target
125+
// Input: Two qubits (stored in an array of length 2) in an arbitrary
126+
// two-qubit state α|00⟩ + β|01⟩ + γ|10⟩ + δ|11⟩.
127+
// Goal: Change the two-qubit state to α|00⟩ + β|01⟩ + δ|10⟩ + γ|11⟩ using only single-qubit gates and joint measurements.
128+
// Do not use two-qubit gates.
129+
operation ControlledX_General (qs : Qubit[]) : ()
130+
{
131+
body
132+
{
133+
// Hint: You can use an extra qubit to perform this operation.
134+
// ...
135+
}
136+
adjoint self;
137+
}
138+
}

0 commit comments

Comments
 (0)