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

Commit a56c47d

Browse files
authored
JointMeasurements: Restrict the use of multi-qubit gates in tasks 6 and 7 (#170)
This is the second part of the fix for #74.
1 parent 0ad4cc4 commit a56c47d

File tree

6 files changed

+65
-17
lines changed

6 files changed

+65
-17
lines changed

JointMeasurements/JointMeasurements.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,8 @@
1919
<ItemGroup>
2020
<None Include="README.md" />
2121
</ItemGroup>
22+
23+
<ItemGroup>
24+
<ProjectReference Include="..\utilities\Common\Common.csproj" />
25+
</ItemGroup>
2226
</Project>

JointMeasurements/JointMeasurements.ipynb

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@
5353
"> </details>\n"
5454
]
5555
},
56+
{
57+
"cell_type": "code",
58+
"execution_count": null,
59+
"metadata": {},
60+
"outputs": [],
61+
"source": [
62+
"%workspace reload"
63+
]
64+
},
5665
{
5766
"cell_type": "markdown",
5867
"metadata": {},
@@ -220,7 +229,7 @@
220229
"source": [
221230
"### Task 7**. Controlled X gate with arbitrary target\n",
222231
"\n",
223-
"**Input:** Two qubits (stored in an array of length 2) in an arbitrary two-qubit state $\\alpha |00\\rangle + \\beta |01\\rangle + \\gamma |10\\rangle + \\delta |11\\rangle$.\n",
232+
"**Input:** Two qubits (stored in an array of length 2) in an arbitrary two-qubit state $\\alpha |00\\rangle + \\beta |01\\rangle + \\color{blue}\\gamma |10\\rangle + \\color{blue}\\delta |11\\rangle$.\n",
224233
"\n",
225234
"**Goal:** Change the two-qubit state to $\\alpha |00\\rangle + \\beta |01\\rangle + \\color{red}\\delta |10\\rangle + \\color{red}\\gamma |11\\rangle$ using only single-qubit gates and joint measurements. Do not use two-qubit gates.\n",
226235
"\n",
@@ -241,12 +250,7 @@
241250
"%kata T07_ControlledX_General_Test\n",
242251
"\n",
243252
"operation ControlledX_General (qs : Qubit[]) : Unit {\n",
244-
" \n",
245-
" body (...) {\n",
246-
" // ...\n",
247-
" }\n",
248-
"\n",
249-
" adjoint self;\n",
253+
" // ...\n",
250254
"}"
251255
]
252256
}

JointMeasurements/JointMeasurements.sln

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 15
44
VisualStudioVersion = 15.0.27130.2036
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JointMeasurements", "JointMeasurements.csproj", "{F7A0175F-4217-4343-8A3C-EA68FAAB6B7B}"
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JointMeasurements", "JointMeasurements.csproj", "{F7A0175F-4217-4343-8A3C-EA68FAAB6B7B}"
7+
EndProject
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Common", "..\utilities\Common\Common.csproj", "{F8640A84-E48D-4C12-BB11-94E709003CE9}"
79
EndProject
810
Global
911
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -15,6 +17,10 @@ Global
1517
{F7A0175F-4217-4343-8A3C-EA68FAAB6B7B}.Debug|Any CPU.Build.0 = Debug|Any CPU
1618
{F7A0175F-4217-4343-8A3C-EA68FAAB6B7B}.Release|Any CPU.ActiveCfg = Release|Any CPU
1719
{F7A0175F-4217-4343-8A3C-EA68FAAB6B7B}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{F8640A84-E48D-4C12-BB11-94E709003CE9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{F8640A84-E48D-4C12-BB11-94E709003CE9}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{F8640A84-E48D-4C12-BB11-94E709003CE9}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{F8640A84-E48D-4C12-BB11-94E709003CE9}.Release|Any CPU.Build.0 = Release|Any CPU
1824
EndGlobalSection
1925
GlobalSection(SolutionProperties) = preSolution
2026
HideSolutionNode = FALSE

JointMeasurements/Tasks.qs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,8 @@ namespace Quantum.Kata.JointMeasurements {
107107
// Goal: Change the two-qubit state to α|00⟩ + β|01⟩ + δ|10⟩ + γ|11⟩ using only single-qubit gates and joint measurements.
108108
// Do not use two-qubit gates.
109109
operation ControlledX_General (qs : Qubit[]) : Unit {
110-
111-
body (...) {
112-
// Hint: You can use an extra qubit to perform this operation.
113-
// ...
114-
}
115-
116-
adjoint self;
110+
// Hint: You can use an extra qubit to perform this operation.
111+
// ...
117112
}
118113

119114
}

JointMeasurements/TestSuiteRunner.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
//////////////////////////////////////////////////////////////////////
99

1010
using Microsoft.Quantum.Simulation.XUnit;
11-
using Microsoft.Quantum.Simulation.Simulators;
1211
using Xunit.Abstractions;
1312
using System.Diagnostics;
1413

14+
using Microsoft.Quantum.Katas;
15+
1516
namespace Quantum.Kata.JointMeasurements
1617
{
1718
public class TestSuiteRunner
@@ -30,7 +31,7 @@ public TestSuiteRunner(ITestOutputHelper output)
3031
[OperationDriver(TestNamespace = "Quantum.Kata.JointMeasurements")]
3132
public void TestTarget(TestOperation op)
3233
{
33-
using (var sim = new QuantumSimulator())
34+
using (var sim = new CounterSimulator())
3435
{
3536
// OnLog defines action(s) performed when Q# test calls function Message
3637
sim.OnLog += (msg) => { output.WriteLine(msg); };

JointMeasurements/Tests.qs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99

1010
namespace Quantum.Kata.JointMeasurements {
1111

12+
open Microsoft.Quantum.Characterization;
1213
open Microsoft.Quantum.Intrinsic;
1314
open Microsoft.Quantum.Canon;
1415
open Microsoft.Quantum.Diagnostics;
1516
open Microsoft.Quantum.Convert;
1617
open Microsoft.Quantum.Math;
1718

19+
open Quantum.Kata.Utils;
1820

1921
// "Framework" operation for testing multi-qubit tasks for distinguishing states of an array of qubits
2022
// with Int return
@@ -164,6 +166,15 @@ namespace Quantum.Kata.JointMeasurements {
164166
}
165167

166168

169+
// ------------------------------------------------------
170+
// An operation to fine-tune universal CounterSimulator
171+
// for the purposes of the last two tasks: prohibiting all multi-qubit operations,
172+
// except the two that are allowed to be used for solving this task
173+
operation GetMultiQubitNonMeasurementOpCount () : Int {
174+
return GetMultiQubitOpCount() - GetOracleCallsCount(Measure) - GetOracleCallsCount(MeasureAllZ);
175+
}
176+
177+
167178
// ------------------------------------------------------
168179
operation T06_ControlledX_Test () : Unit {
169180
// Note that the way the problem is formulated, we can't just compare two unitaries,
@@ -176,9 +187,15 @@ namespace Quantum.Kata.JointMeasurements {
176187
// prepare A state
177188
StatePrep_A(alpha, qs[0]);
178189

190+
ResetOracleCallsCount();
191+
179192
// apply operation that needs to be tested
180193
ControlledX(qs);
181194

195+
// the 1 in the following condition is the task operation itself being called
196+
Fact(GetMultiQubitNonMeasurementOpCount() <= 1,
197+
"You are not allowed to use multi-qubit gates in this task.");
198+
182199
// apply adjoint reference operation and adjoint of state prep
183200
CNOT(qs[0], qs[1]);
184201
Adjoint StatePrep_A(alpha, qs[0]);
@@ -205,6 +222,27 @@ namespace Quantum.Kata.JointMeasurements {
205222
// In this task the gate is supposed to work on all inputs, so we can compare the unitary to CNOT.
206223
AssertOperationsEqualReferenced(2, CNOTWrapper, ControlledX_General_Reference);
207224
AssertOperationsEqualReferenced(2, ControlledX_General, ControlledX_General_Reference);
225+
226+
// Check that the implementation of ControlledX_General doesn't call multi-qubit gates (other than itself)
227+
using (qs = Qubit[2]) {
228+
// prepare a non-trivial input state
229+
ApplyToEach(H, qs);
230+
231+
ResetOracleCallsCount();
232+
233+
ControlledX_General(qs);
234+
235+
// the 1 in the following condition is the task operation itself being called
236+
Fact(GetMultiQubitNonMeasurementOpCount() <= 1,
237+
"You are not allowed to use multi-qubit gates in this task.");
238+
239+
// apply adjoint reference operation and adjoint of state prep
240+
CNOT(qs[0], qs[1]);
241+
ApplyToEach(H, qs);
242+
243+
// assert that all qubits end up in |0⟩ state
244+
AssertAllZero(qs);
245+
}
208246
}
209247

210248
}

0 commit comments

Comments
 (0)