From d377839b1639f3042f483c526657753521aa3d7a Mon Sep 17 00:00:00 2001 From: Scott Carda Date: Wed, 12 Aug 2020 12:01:04 -0700 Subject: [PATCH 1/6] Replaced the nullable member access with regular member access for Slice. --- src/Simulation/CsharpGeneration/SimulationCode.fs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Simulation/CsharpGeneration/SimulationCode.fs b/src/Simulation/CsharpGeneration/SimulationCode.fs index 30d8bcdd95a..8c44f9a3a81 100644 --- a/src/Simulation/CsharpGeneration/SimulationCode.fs +++ b/src/Simulation/CsharpGeneration/SimulationCode.fs @@ -561,7 +561,7 @@ module SimulationCode = and buildArrayItem a i = match i.ResolvedType.Resolution with - | Range -> ``invoke`` ((buildExpression a) <|?.|> (``ident`` "Slice")) ``(`` [ (buildExpression i) ] ``)`` + | Range -> ``invoke`` ((buildExpression a) <|.|> (``ident`` "Slice")) ``(`` [ (buildExpression i) ] ``)`` | _ -> ``item`` (buildExpression a) [ (buildExpression i) ] let buildBlock (block : QsScope) = From 174c35d3588ff9e9d719db47b23e5ab18ca3bb19 Mon Sep 17 00:00:00 2001 From: Scott Carda Date: Wed, 12 Aug 2020 14:55:15 -0700 Subject: [PATCH 2/6] removing debug statements --- src/Simulation/Common/QubitManager.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Simulation/Common/QubitManager.cs b/src/Simulation/Common/QubitManager.cs index a70651f5059..dc3e84f7c3d 100644 --- a/src/Simulation/Common/QubitManager.cs +++ b/src/Simulation/Common/QubitManager.cs @@ -104,7 +104,7 @@ private void ExtendQubitArray() { if (oldQubitsArray[i] == oldNone) { // Point to the first new (free) element - Debug.Assert(false,"Why do we extend an array, when we still have available slots?"); + //Debug.Assert(false,"Why do we extend an array, when we still have available slots?"); this.qubits[i] = oldNumQubits; } else if (oldQubitsArray[i] == oldAllocated) { // Allocated qubits are marked differently now. @@ -133,7 +133,7 @@ private void ExtendQubitArray() freeTail = NumQubits - 1; } else { - Debug.Assert(false, "Why do we extend an array, when we still have available slots?"); + //Debug.Assert(false, "Why do we extend an array, when we still have available slots?"); } } From 0a21f61044e5e3131f8bbef12948abf4747f7aaf Mon Sep 17 00:00:00 2001 From: Scott Carda Date: Wed, 12 Aug 2020 15:12:20 -0700 Subject: [PATCH 3/6] Update generated C# code tests --- .../SimulationCodeTests.fs | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Simulation/CsharpGeneration.Tests/SimulationCodeTests.fs b/src/Simulation/CsharpGeneration.Tests/SimulationCodeTests.fs index de97e3aedc8..46c44ec10a7 100644 --- a/src/Simulation/CsharpGeneration.Tests/SimulationCodeTests.fs +++ b/src/Simulation/CsharpGeneration.Tests/SimulationCodeTests.fs @@ -1242,7 +1242,7 @@ namespace N1 "X.Apply(qubits.Data[0L]);" "X.Adjoint.Apply(qubits.Data[0L]);" - "X.Controlled.Apply((qubits.Data?.Slice(new QRange(1L,5L)), qubits.Data[0L]));" + "X.Controlled.Apply((qubits.Data.Slice(new QRange(1L,5L)), qubits.Data[0L]));" "call_target1.Apply((1L, X, X, X, X));" "call_target1.Apply((1L, plain.Data, adj.Data, ctr.Data, uni.Data));" @@ -1820,7 +1820,7 @@ namespace N1 "var r5 = (IQArray)QArray.Create((4L + 2L));" "var r6 = QArray.Create(r5.Length);" "var r7 = (IQArray)QArray.Add(r2, r4);" - "var r8 = (IQArray)r7?.Slice(new QRange(1L, 5L, 10L));" + "var r8 = (IQArray)r7.Slice(new QRange(1L, 5L, 10L));" "var r9 = new arrays_T1(new QArray(Pauli.PauliX, Pauli.PauliY));" "var r10 = (IQArray)QArray.Create(4L);" @@ -1828,8 +1828,8 @@ namespace N1 "var r12 = (IQArray)QArray.Create(r10.Length);" "var r13 = new arrays_T3(new QArray>(new QArray(Result.Zero, Result.One), new QArray(Result.One, Result.Zero)));" "var r14 = (IQArray)QArray.Add(qubits, register.Data);" - "var r15 = (IQArray)register.Data?.Slice(new QRange(0L, 2L));" - "var r16 = (IQArray)qubits?.Slice(new QRange(1L, -(1L)));" + "var r15 = (IQArray)register.Data.Slice(new QRange(0L, 2L));" + "var r16 = (IQArray)qubits.Slice(new QRange(1L, -(1L)));" "var r18 = (IQArray)QArray.Create(2L);" "var r19 = (IQArray)QArray.Create(7L);" "var i0 = r13.Data[0L][1L];" @@ -1860,12 +1860,12 @@ namespace N1 "var r2 = new QRange(10L,-(2L),0L);" "var ranges = (IQArray)QArray.Create(1L);" - "var s1 = (IQArray)qubits?.Slice(new QRange(0L,10L));" - "var s2 = (IQArray)qubits?.Slice(r2);" - "var s3 = (IQArray)qubits?.Slice(ranges[3L]);" - "var s4 = (IQArray)qubits?.Slice(GetMeARange.Apply(QVoid.Instance));" + "var s1 = (IQArray)qubits.Slice(new QRange(0L,10L));" + "var s2 = (IQArray)qubits.Slice(r2);" + "var s3 = (IQArray)qubits.Slice(ranges[3L]);" + "var s4 = (IQArray)qubits.Slice(GetMeARange.Apply(QVoid.Instance));" - "return qubits?.Slice(new QRange(10L,-(3L),0L));" + "return qubits.Slice(new QRange(10L,-(3L),0L));" ] |> testOneBody (applyVisitor sliceOperations) @@ -3529,7 +3529,7 @@ namespace Microsoft.Quantum.Tests.LineNumbers else { #line 20 "%%" - foreach (var c in ctrls?.Slice(new QRange(0L, 2L, r))) + foreach (var c in ctrls.Slice(new QRange(0L, 2L, r))) #line hidden { #line 21 "%%" From 1f00228e53c4744527b9704f24b16e8ce373f1cb Mon Sep 17 00:00:00 2001 From: Scott Carda Date: Wed, 12 Aug 2020 16:17:50 -0700 Subject: [PATCH 4/6] Removing erroneous debug asserts --- src/Simulation/Common/QubitManager.cs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/Simulation/Common/QubitManager.cs b/src/Simulation/Common/QubitManager.cs index dc3e84f7c3d..9bc54dfce3d 100644 --- a/src/Simulation/Common/QubitManager.cs +++ b/src/Simulation/Common/QubitManager.cs @@ -104,7 +104,6 @@ private void ExtendQubitArray() { if (oldQubitsArray[i] == oldNone) { // Point to the first new (free) element - //Debug.Assert(false,"Why do we extend an array, when we still have available slots?"); this.qubits[i] = oldNumQubits; } else if (oldQubitsArray[i] == oldAllocated) { // Allocated qubits are marked differently now. @@ -131,9 +130,6 @@ private void ExtendQubitArray() { free = oldNumQubits; freeTail = NumQubits - 1; - } else - { - //Debug.Assert(false, "Why do we extend an array, when we still have available slots?"); } } From 6cdddd68fd74cb95e17e4c32edb8ffef34f59b2d Mon Sep 17 00:00:00 2001 From: Scott Carda Date: Thu, 13 Aug 2020 10:48:41 -0700 Subject: [PATCH 5/6] put the debug asserts back in --- src/Simulation/Common/QubitManager.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Simulation/Common/QubitManager.cs b/src/Simulation/Common/QubitManager.cs index 9bc54dfce3d..a70651f5059 100644 --- a/src/Simulation/Common/QubitManager.cs +++ b/src/Simulation/Common/QubitManager.cs @@ -104,6 +104,7 @@ private void ExtendQubitArray() { if (oldQubitsArray[i] == oldNone) { // Point to the first new (free) element + Debug.Assert(false,"Why do we extend an array, when we still have available slots?"); this.qubits[i] = oldNumQubits; } else if (oldQubitsArray[i] == oldAllocated) { // Allocated qubits are marked differently now. @@ -130,6 +131,9 @@ private void ExtendQubitArray() { free = oldNumQubits; freeTail = NumQubits - 1; + } else + { + Debug.Assert(false, "Why do we extend an array, when we still have available slots?"); } } From 040045318badaca2c573cf575730fdd6cbd5c4bf Mon Sep 17 00:00:00 2001 From: Scott Carda Date: Thu, 13 Aug 2020 17:40:38 -0700 Subject: [PATCH 6/6] Added a test from the bug description. --- .../Simulators.Tests/Circuits/Issue132.qs | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/Simulation/Simulators.Tests/Circuits/Issue132.qs diff --git a/src/Simulation/Simulators.Tests/Circuits/Issue132.qs b/src/Simulation/Simulators.Tests/Circuits/Issue132.qs new file mode 100644 index 00000000000..9b57996913a --- /dev/null +++ b/src/Simulation/Simulators.Tests/Circuits/Issue132.qs @@ -0,0 +1,25 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +namespace Microsoft.Quantum.Simulation.Simulators.Tests.Circuits +{ + open Microsoft.Quantum.Intrinsic; + + operation SliceGenerationTest() : Unit { + using (qs = Qubit[4]) { + PrepareCatState(qs); + if (M(qs[0]) == One) { + for (target in qs) { + X(target); + } + } + } + } + + operation PrepareCatState(register : Qubit[]) : Unit is Adj + Ctl { + H(register[0]); + for (target in register[1...]) { + CNOT(register[0], target); + } + } +}