From a1cfb618871f4a851a570c7ec129d7cd96c3f7da Mon Sep 17 00:00:00 2001 From: Christopher Granade Date: Wed, 24 Jun 2020 08:43:45 -0700 Subject: [PATCH 1/8] Move NoOp.qs from QuantumLibraries. --- src/Simulation/QsharpCore/Canon/NoOp.qs | 45 +++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/Simulation/QsharpCore/Canon/NoOp.qs diff --git a/src/Simulation/QsharpCore/Canon/NoOp.qs b/src/Simulation/QsharpCore/Canon/NoOp.qs new file mode 100644 index 00000000000..b9a778f6e26 --- /dev/null +++ b/src/Simulation/QsharpCore/Canon/NoOp.qs @@ -0,0 +1,45 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +namespace Microsoft.Quantum.Canon { + + /// # Summary + /// Performs the identity operation (no-op) on an argument. + /// + /// # Description + /// This operation takes a value of any type and does nothing to it. + /// This can be useful whenever an input of an operation type is expected, + /// but no action should be taken. + /// For instance, if a particular error correction syndrome indicates that + /// no error has occurred, `NoOp` may be the correct recovery + /// procedure. + /// Similarly, if an operation expects a state preparation procedure as + /// input, `NoOp` can be used to prepare the state + /// $\ket{0 \cdots 0}$. + /// + /// # Input + /// ## input + /// A value to be ignored. + /// + /// # Remarks + /// In almost all cases, the type parameter for `NoOp` needs to be specified + /// explicitly. For instance, `NoOp` is identical to + /// . + /// + /// # See Also + /// - Microsoft.Quantum.Intrinsic.I + operation NoOp<'T>(input : 'T) : Unit is Adj + Ctl { + } + + + /// # Summary + /// Ignores the output of an operation or function. + /// + /// # Input + /// ## value + /// A value to be ignored. + function Ignore<'T> (value : 'T) : Unit { + return (); + } + +} From c796f395f1faa601e3481ace79eb11bbbd12b5a2 Mon Sep 17 00:00:00 2001 From: Christopher Granade Date: Wed, 24 Jun 2020 08:43:59 -0700 Subject: [PATCH 2/8] Remove deprecated primitives. --- src/Simulation/QsharpCore/Primitive.qs | 329 ------------------------- 1 file changed, 329 deletions(-) delete mode 100644 src/Simulation/QsharpCore/Primitive.qs diff --git a/src/Simulation/QsharpCore/Primitive.qs b/src/Simulation/QsharpCore/Primitive.qs deleted file mode 100644 index 85fb806872d..00000000000 --- a/src/Simulation/QsharpCore/Primitive.qs +++ /dev/null @@ -1,329 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -namespace Microsoft.Quantum.Primitive { - - /// # Deprecated - /// Please use @"microsoft.quantum.intrinsic.random". - @Deprecated("Microsoft.Quantum.Intrinsic.Random") - operation Random (probs : Double[]) : Int { - return Microsoft.Quantum.Intrinsic.Random(probs); - } - - /// # Deprecated - /// Please use @"microsoft.quantum.intrinsic.assert". - @Deprecated("Microsoft.Quantum.Intrinsic.Assert") - operation Assert (bases : Pauli[], qubits : Qubit[], result : Result, msg : String) : Unit { - body (...) { - Microsoft.Quantum.Intrinsic.Assert(bases, qubits, result, msg); - } - adjoint auto; - controlled auto; - controlled adjoint auto; - } - - /// # Deprecated - /// Please use @"microsoft.quantum.intrinsic.assertprob". - @Deprecated("Microsoft.Quantum.Intrinsic.AssertProb") - operation AssertProb (bases : Pauli[], qubits : Qubit[], result : Result, prob : Double, msg : String, tol : Double) : Unit - { - body (...) { - Microsoft.Quantum.Intrinsic.AssertProb(bases, qubits, result, prob, msg, tol); - } - adjoint auto; - controlled auto; - controlled adjoint auto; - } - - /// # Deprecated - /// Please use @"microsoft.quantum.intrinsic.message". - @Deprecated("Microsoft.Quantum.Intrinsic.Message") - function Message (msg : String) : Unit { - return Microsoft.Quantum.Intrinsic.Message(msg); - } - - /// # Deprecated - /// Please use @"microsoft.quantum.intrinsic.i". - @Deprecated("Microsoft.Quantum.Intrinsic.I") - operation I (qubit : Qubit) : Unit { - body (...) { - Microsoft.Quantum.Intrinsic.I(qubit); - } - adjoint self; - controlled auto; - controlled adjoint self; - } - - /// # Deprecated - /// Please use @"microsoft.quantum.intrinsic.x". - @Deprecated("Microsoft.Quantum.Intrinsic.X") - operation X (qubit : Qubit) : Unit { - body (...) { - Microsoft.Quantum.Intrinsic.X(qubit); - } - adjoint self; - controlled auto; - controlled adjoint self; - } - - /// # Deprecated - /// Please use @"microsoft.quantum.intrinsic.y". - @Deprecated("Microsoft.Quantum.Intrinsic.Y") - operation Y (qubit : Qubit) : Unit { - body (...) { - Microsoft.Quantum.Intrinsic.Y(qubit); - } - adjoint self; - controlled auto; - controlled adjoint self; - } - - /// # Deprecated - /// Please use @"microsoft.quantum.intrinsic.z". - @Deprecated("Microsoft.Quantum.Intrinsic.Z") - operation Z (qubit : Qubit) : Unit { - body (...) { - Microsoft.Quantum.Intrinsic.Z(qubit); - } - adjoint self; - controlled auto; - controlled adjoint self; - } - - /// # Deprecated - /// Please use @"microsoft.quantum.intrinsic.h". - @Deprecated("Microsoft.Quantum.Intrinsic.H") - operation H (qubit : Qubit) : Unit { - body (...) { - Microsoft.Quantum.Intrinsic.H(qubit); - } - adjoint self; - controlled auto; - controlled adjoint self; - } - - /// # Deprecated - /// Please use @"microsoft.quantum.intrinsic.s". - @Deprecated("Microsoft.Quantum.Intrinsic.S") - operation S (qubit : Qubit) : Unit { - body (...) { - Microsoft.Quantum.Intrinsic.S(qubit); - } - adjoint auto; - controlled auto; - controlled adjoint auto; - } - - /// # Deprecated - /// Please use @"microsoft.quantum.intrinsic.t". - @Deprecated("Microsoft.Quantum.Intrinsic.T") - operation T (qubit : Qubit) : Unit { - body (...) { - Microsoft.Quantum.Intrinsic.T(qubit); - } - adjoint auto; - controlled auto; - controlled adjoint auto; - } - - /// # Deprecated - /// Please use @"microsoft.quantum.intrinsic.cnot". - @Deprecated("Microsoft.Quantum.Intrinsic.CNOT") - operation CNOT (control : Qubit, target : Qubit) : Unit { - body (...) { - Microsoft.Quantum.Intrinsic.CNOT(control, target); - } - - adjoint auto; - controlled auto; - controlled adjoint auto; - } - - /// # Deprecated - /// Please use @"microsoft.quantum.intrinsic.ccnot". - @Deprecated("Microsoft.Quantum.Intrinsic.CCNOT") - operation CCNOT (control1 : Qubit, control2 : Qubit, target : Qubit) : Unit { - body (...) { - Microsoft.Quantum.Intrinsic.CCNOT(control1, control2, target); - } - - adjoint auto; - controlled auto; - controlled adjoint auto; - } - - /// # Deprecated - /// Please use @"microsoft.quantum.intrinsic.swap". - @Deprecated("Microsoft.Quantum.Intrinsic.SWAP") - operation SWAP (qubit1 : Qubit, qubit2 : Qubit) : Unit { - body (...) { - Microsoft.Quantum.Intrinsic.SWAP(qubit1, qubit2); - } - - adjoint auto; - controlled auto; - controlled adjoint auto; - } - - /// # Deprecated - /// Please use @"microsoft.quantum.canon.applytoeachca". - @Deprecated("ApplyToEachCA(X, _)") - operation MultiX (qubits : Qubit[]) : Unit { - body (...) { - for (index in 0 .. Length(qubits) - 1) { - Microsoft.Quantum.Intrinsic.X(qubits[index]); - } - } - - adjoint self; - controlled distribute; - controlled adjoint self; - } - - /// # Deprecated - /// Please use @"microsoft.quantum.intrinsic.r". - @Deprecated("Microsoft.Quantum.Intrinsic.R") - operation R(pauli : Pauli, theta : Double, qubit : Qubit) : Unit { - body (...) { - Microsoft.Quantum.Intrinsic.R(pauli, theta, qubit); - } - adjoint auto; - controlled auto; - controlled adjoint auto; - } - - /// # Deprecated - /// Please use @"microsoft.quantum.intrinsic.rfrac". - @Deprecated("Microsoft.Quantum.Intrinsic.RFrac") - operation RFrac(pauli : Pauli, numerator : Int, power : Int, qubit : Qubit) : Unit { - body (...) { - Microsoft.Quantum.Intrinsic.RFrac(pauli, numerator, power, qubit); - } - - adjoint auto; - controlled auto; - controlled adjoint auto; - } - - - /// # Deprecated - /// Please use @"microsoft.quantum.intrinsic.rx". - @Deprecated("Microsoft.Quantum.Intrinsic.Rx") - operation Rx (theta : Double, qubit : Qubit) : Unit { - body (...) { - Microsoft.Quantum.Intrinsic.Rx(theta, qubit); - } - - adjoint auto; - controlled auto; - controlled adjoint auto; - } - - /// # Deprecated - /// Please use @"microsoft.quantum.intrinsic.ry". - @Deprecated("Microsoft.Quantum.Intrinsic.Ry") - operation Ry (theta : Double, qubit : Qubit) : Unit { - body (...) { - Microsoft.Quantum.Intrinsic.Ry(theta, qubit); - } - - adjoint auto; - controlled auto; - controlled adjoint auto; - } - - /// # Deprecated - /// Please use @"microsoft.quantum.intrinsic.rz". - @Deprecated("Microsoft.Quantum.Intrinsic.Rz") - operation Rz (theta : Double, qubit : Qubit) : Unit { - body (...) { - Microsoft.Quantum.Intrinsic.Rz(theta, qubit); - } - - adjoint auto; - controlled auto; - controlled adjoint auto; - } - - /// # Deprecated - /// Please use @"microsoft.quantum.intrinsic.r1". - @Deprecated("Microsoft.Quantum.Intrinsic.R1") - operation R1 (theta : Double, qubit : Qubit) : Unit - { - body (...) { - Microsoft.Quantum.Intrinsic.R1(theta, qubit); - } - - adjoint auto; - controlled auto; - controlled adjoint auto; - } - - /// # Deprecated - /// Please use @"microsoft.quantum.intrinsic.r1frac". - @Deprecated("Microsoft.Quantum.Intrinsic.R1Frac") - operation R1Frac (numerator : Int, power : Int, qubit : Qubit) : Unit - { - body (...) { - Microsoft.Quantum.Intrinsic.R1Frac(numerator, power, qubit); - } - - adjoint auto; - controlled auto; - controlled adjoint auto; - } - - /// # Deprecated - /// Please use @"microsoft.quantum.intrinsic.exp". - @Deprecated("Microsoft.Quantum.Intrinsic.Exp") - operation Exp (paulis : Pauli[], theta : Double, qubits : Qubit[]) : Unit { - body (...) { - Microsoft.Quantum.Intrinsic.Exp(paulis, theta, qubits); - } - adjoint auto; - controlled auto; - controlled adjoint auto; - } - - /// # Deprecated - /// Please use @"microsoft.quantum.intrinsic.expfrac". - @Deprecated("Microsoft.Quantum.Intrinsic.ExpFrac") - operation ExpFrac (paulis : Pauli[], numerator : Int, power : Int, qubits : Qubit[]) : Unit { - body (...) { - Microsoft.Quantum.Intrinsic.ExpFrac(paulis, numerator, power, qubits); - } - adjoint auto; - controlled auto; - controlled adjoint auto; - } - - /// # Deprecated - /// Please use @"microsoft.quantum.intrinsic.measure". - @Deprecated("Microsoft.Quantum.Intrinsic.Measure") - operation Measure(bases : Pauli[], qubits : Qubit[]) : Result { - return Microsoft.Quantum.Intrinsic.Measure(bases, qubits); - } - - /// # Deprecated - /// Please use @"microsoft.quantum.intrinsic.m". - @Deprecated("Microsoft.Quantum.Intrinsic.M") - operation M (qubit : Qubit) : Result { - return Microsoft.Quantum.Intrinsic.M(qubit); - } - - /// # Deprecated - /// Please use @"microsoft.quantum.intrinsic.reset". - @Deprecated("Microsoft.Quantum.Intrinsic.Reset") - operation Reset (target : Qubit) : Unit { - return Microsoft.Quantum.Intrinsic.Reset(target); - } - - /// # Deprecated - /// Please use @"microsoft.quantum.intrinsic.resetall". - @Deprecated("Microsoft.Quantum.Intrinsic.ResetAll") - operation ResetAll (qubits : Qubit[]) : Unit { - return Microsoft.Quantum.Intrinsic.ResetAll(qubits); - } - -} - From 306f93f9f0df6b43ae72480c266d29b317ef0b05 Mon Sep 17 00:00:00 2001 From: Christopher Granade Date: Wed, 24 Jun 2020 08:44:12 -0700 Subject: [PATCH 3/8] Move diagnostics into diagnostics namespace. --- .../QsharpCore/Diagnostics/Assert.qs | 69 +++++++++++++++++++ src/Simulation/QsharpCore/Intrinsic.qs | 60 ++-------------- .../QCTraceSimulator.Primitive.Assert.cs | 2 +- .../QCTraceSimulator.Primitive.AssertProb.cs | 2 +- .../Simulators/QuantumProcessor/Assert.cs | 2 +- .../Simulators/QuantumProcessor/AssertProb.cs | 2 +- .../Simulators/QuantumSimulator/Assert.cs | 2 +- .../Simulators/QuantumSimulator/AssertProb.cs | 2 +- .../Simulators/ToffoliSimulator/Assert.cs | 2 +- .../Simulators/ToffoliSimulator/AssertProb.cs | 2 +- 10 files changed, 81 insertions(+), 64 deletions(-) create mode 100644 src/Simulation/QsharpCore/Diagnostics/Assert.qs diff --git a/src/Simulation/QsharpCore/Diagnostics/Assert.qs b/src/Simulation/QsharpCore/Diagnostics/Assert.qs new file mode 100644 index 00000000000..08e871a9443 --- /dev/null +++ b/src/Simulation/QsharpCore/Diagnostics/Assert.qs @@ -0,0 +1,69 @@ +namespace Microsoft.Quantum.Diagnostics { + open Microsoft.Quantum.Intrinsic; + open Microsoft.Quantum.Canon; + + /// # Summary + /// Asserts that measuring the given qubits in the given Pauli basis will + /// always have the given result. + /// + /// # Input + /// ## bases + /// A measurement effect to assert the probability of, expressed as a + /// multi-qubit Pauli operator. + /// ## qubits + /// A register on which to make the assertion. + /// ## result + /// The expected result of `Measure(bases, qubits)`. + /// ## msg + /// A message to be reported if the assertion fails. + /// + /// # Remarks + /// Note that the Adjoint and Controlled versions of this operation will not + /// check the condition. + /// + /// # See Also + /// - Microsoft.Quantum.Diagnostics.AssertMeasurementProbability + operation AssertMeasurement(bases : Pauli[], qubits : Qubit[], result : Result, msg : String) : Unit + is Adj + Ctl { + body intrinsic; + } + + + /// # Summary + /// Asserts that measuring the given qubits in the given Pauli basis will have the given result + /// with the given probability, within some tolerance. + /// + /// # Input + /// ## bases + /// A measurement effect to assert the probability of, expressed as a + /// multi-qubit Pauli operator. + /// ## qubits + /// A register on which to make the assertion. + /// ## result + /// An expected result of `Measure(bases, qubits)`. + /// ## prob + /// The probability with which the given result is expected. + /// ## msg + /// A message to be reported if the assertion fails. + /// + /// # Example + /// ```qsharp + /// using (register = Qubit()) { + /// H(register); + /// AssertProb([PauliZ], [register], One, 0.5, + /// "Measuring in conjugate basis did not give 50/50 results.", 1e-5); + /// } + /// ``` + /// + /// # Remarks + /// Note that the Adjoint and Controlled versions of this operation will not + /// check the condition. + /// + /// # See Also + /// - Microsoft.Quantum.Diagnostics.AssertMeasurement + operation AssertMeasurementProbability(bases : Pauli[], qubits : Qubit[], result : Result, prob : Double, msg : String, tol : Double) : Unit + is Adj + Ctl { + body intrinsic; + } + +} diff --git a/src/Simulation/QsharpCore/Intrinsic.qs b/src/Simulation/QsharpCore/Intrinsic.qs index 28f0353dd86..35412b08e6a 100644 --- a/src/Simulation/QsharpCore/Intrinsic.qs +++ b/src/Simulation/QsharpCore/Intrinsic.qs @@ -27,68 +27,16 @@ namespace Microsoft.Quantum.Intrinsic { } - /// # Summary - /// Asserts that measuring the given qubits in the given Pauli basis will - /// always have the given result. - /// - /// # Input - /// ## bases - /// A measurement effect to assert the probability of, expressed as a - /// multi-qubit Pauli operator. - /// ## qubits - /// A register on which to make the assertion. - /// ## result - /// The expected result of `Measure(bases, qubits)`. - /// ## msg - /// A message to be reported if the assertion fails. - /// - /// # Remarks - /// Note that the Adjoint and Controlled versions of this operation will not - /// check the condition. - /// - /// # See Also - /// - AssertProb + @Deprecated("Microsoft.Quantum.Diagnostics.AssertMeasurement") operation Assert (bases : Pauli[], qubits : Qubit[], result : Result, msg : String) : Unit is Adj + Ctl { - body intrinsic; + Microsoft.Quantum.Diagnostics.AssertMeasurement(bases, qubits, result, msg); } - - /// # Summary - /// Asserts that measuring the given qubits in the given Pauli basis will have the given result - /// with the given probability, within some tolerance. - /// - /// # Input - /// ## bases - /// A measurement effect to assert the probability of, expressed as a - /// multi-qubit Pauli operator. - /// ## qubits - /// A register on which to make the assertion. - /// ## result - /// An expected result of `Measure(bases, qubits)`. - /// ## prob - /// The probability with which the given result is expected. - /// ## msg - /// A message to be reported if the assertion fails. - /// - /// # Example - /// ```qsharp - /// using (register = Qubit()) { - /// H(register); - /// AssertProb([PauliZ], [register], One, 0.5, - /// "Measuring in conjugate basis did not give 50/50 results.", 1e-5); - /// } - /// ``` - /// - /// # Remarks - /// Note that the Adjoint and Controlled versions of this operation will not - /// check the condition. - /// - /// # See Also - /// - Assert + @Deprecated("Microsoft.Quantum.Diagnostics.AssertMeasurementProbability") operation AssertProb (bases : Pauli[], qubits : Qubit[], result : Result, prob : Double, msg : String, tol : Double) : Unit is Adj + Ctl { - body intrinsic; + Microsoft.Quantum.Diagnostics.AssertMeasurementProbability(bases, qubits, result, prob, msg, tol); } diff --git a/src/Simulation/Simulators/QCTraceSimulator/QCTraceSimulator.Primitive.Assert.cs b/src/Simulation/Simulators/QCTraceSimulator/QCTraceSimulator.Primitive.Assert.cs index bcbd49b948a..71357de0bd7 100644 --- a/src/Simulation/Simulators/QCTraceSimulator/QCTraceSimulator.Primitive.Assert.cs +++ b/src/Simulation/Simulators/QCTraceSimulator/QCTraceSimulator.Primitive.Assert.cs @@ -9,7 +9,7 @@ namespace Microsoft.Quantum.Simulation.Simulators.QCTraceSimulators.Implementati public partial class QCTraceSimulatorImpl { - public class TracerAssert : Intrinsic.Assert + public class TracerAssert : Microsoft.Quantum.Diagnostics.AssertMeasurement { private readonly QCTraceSimulatorCore core; public TracerAssert(QCTraceSimulatorImpl m) : base(m) diff --git a/src/Simulation/Simulators/QCTraceSimulator/QCTraceSimulator.Primitive.AssertProb.cs b/src/Simulation/Simulators/QCTraceSimulator/QCTraceSimulator.Primitive.AssertProb.cs index 511682661ed..705d5d3ae1d 100644 --- a/src/Simulation/Simulators/QCTraceSimulator/QCTraceSimulator.Primitive.AssertProb.cs +++ b/src/Simulation/Simulators/QCTraceSimulator/QCTraceSimulator.Primitive.AssertProb.cs @@ -9,7 +9,7 @@ namespace Microsoft.Quantum.Simulation.Simulators.QCTraceSimulators.Implementati public partial class QCTraceSimulatorImpl { - public class TracerAssertProb : Intrinsic.AssertProb + public class TracerAssertProb : Microsoft.Quantum.Diagnostics.AssertMeasurementProbability { private readonly QCTraceSimulatorCore core; public TracerAssertProb(QCTraceSimulatorImpl m) : base(m){ diff --git a/src/Simulation/Simulators/QuantumProcessor/Assert.cs b/src/Simulation/Simulators/QuantumProcessor/Assert.cs index 50f230e89ee..a0badd62007 100644 --- a/src/Simulation/Simulators/QuantumProcessor/Assert.cs +++ b/src/Simulation/Simulators/QuantumProcessor/Assert.cs @@ -9,7 +9,7 @@ namespace Microsoft.Quantum.Simulation.QuantumProcessor { public partial class QuantumProcessorDispatcher { - public class QuantumProcessorDispatcherAssert : Quantum.Intrinsic.Assert + public class QuantumProcessorDispatcherAssert : Microsoft.Quantum.Diagnostics.AssertMeasurement { private QuantumProcessorDispatcher Simulator { get; } diff --git a/src/Simulation/Simulators/QuantumProcessor/AssertProb.cs b/src/Simulation/Simulators/QuantumProcessor/AssertProb.cs index 9a86fb29fca..9f4d87e085e 100644 --- a/src/Simulation/Simulators/QuantumProcessor/AssertProb.cs +++ b/src/Simulation/Simulators/QuantumProcessor/AssertProb.cs @@ -9,7 +9,7 @@ namespace Microsoft.Quantum.Simulation.QuantumProcessor { public partial class QuantumProcessorDispatcher { - public class QuantumProcessorDispatcherAssertProb : Quantum.Intrinsic.AssertProb + public class QuantumProcessorDispatcherAssertProb : Microsoft.Quantum.Diagnostics.AssertMeasurementProbability { private QuantumProcessorDispatcher Simulator { get; } diff --git a/src/Simulation/Simulators/QuantumSimulator/Assert.cs b/src/Simulation/Simulators/QuantumSimulator/Assert.cs index d0936509470..69d1b035d69 100644 --- a/src/Simulation/Simulators/QuantumSimulator/Assert.cs +++ b/src/Simulation/Simulators/QuantumSimulator/Assert.cs @@ -12,7 +12,7 @@ namespace Microsoft.Quantum.Simulation.Simulators { public partial class QuantumSimulator { - public class QSimAssert : Quantum.Intrinsic.Assert + public class QSimAssert : Microsoft.Quantum.Diagnostics.AssertMeasurement { [DllImport(QSIM_DLL_NAME, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, EntryPoint = "JointEnsembleProbability")] private static extern double JointEnsembleProbability(uint id, uint n, Pauli[] b, uint[] q); diff --git a/src/Simulation/Simulators/QuantumSimulator/AssertProb.cs b/src/Simulation/Simulators/QuantumSimulator/AssertProb.cs index 8f93eea3b31..39e3901e592 100644 --- a/src/Simulation/Simulators/QuantumSimulator/AssertProb.cs +++ b/src/Simulation/Simulators/QuantumSimulator/AssertProb.cs @@ -12,7 +12,7 @@ namespace Microsoft.Quantum.Simulation.Simulators { public partial class QuantumSimulator { - public class QSimAssertProb : Quantum.Intrinsic.AssertProb + public class QSimAssertProb : Microsoft.Quantum.Diagnostics.AssertMeasurementProbability { [DllImport(QSIM_DLL_NAME, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, EntryPoint = "JointEnsembleProbability")] private static extern double JointEnsembleProbability(uint id, uint n, Pauli[] b, uint[] q); diff --git a/src/Simulation/Simulators/ToffoliSimulator/Assert.cs b/src/Simulation/Simulators/ToffoliSimulator/Assert.cs index 1d00d159ea1..13c71ead4d9 100644 --- a/src/Simulation/Simulators/ToffoliSimulator/Assert.cs +++ b/src/Simulation/Simulators/ToffoliSimulator/Assert.cs @@ -12,7 +12,7 @@ public partial class ToffoliSimulator /// /// Implementation of the Assert operation for the Toffoli simulator. /// - public class Assert : Intrinsic.Assert + public class Assert : Microsoft.Quantum.Diagnostics.AssertMeasurement { private ToffoliSimulator simulator; diff --git a/src/Simulation/Simulators/ToffoliSimulator/AssertProb.cs b/src/Simulation/Simulators/ToffoliSimulator/AssertProb.cs index 29228e9b604..3ed31f98369 100644 --- a/src/Simulation/Simulators/ToffoliSimulator/AssertProb.cs +++ b/src/Simulation/Simulators/ToffoliSimulator/AssertProb.cs @@ -13,7 +13,7 @@ public partial class ToffoliSimulator /// /// Implementation of the AssertProb operation for the Toffoli simulator. /// - public class AssertProb : Intrinsic.AssertProb + public class AssertProb : Microsoft.Quantum.Diagnostics.AssertMeasurementProbability { private ToffoliSimulator simulator; From b1925ce13556bfc74a85fe59d57661fb24dbf2a4 Mon Sep 17 00:00:00 2001 From: Christopher Granade Date: Wed, 24 Jun 2020 09:11:23 -0700 Subject: [PATCH 4/8] Adapt AB#3758 regression test. --- .../Simulators.Tests/Circuits/Bug3758.qs | 46 +++++++------------ 1 file changed, 16 insertions(+), 30 deletions(-) diff --git a/src/Simulation/Simulators.Tests/Circuits/Bug3758.qs b/src/Simulation/Simulators.Tests/Circuits/Bug3758.qs index 894c45cac3e..e65347a39e1 100644 --- a/src/Simulation/Simulators.Tests/Circuits/Bug3758.qs +++ b/src/Simulation/Simulators.Tests/Circuits/Bug3758.qs @@ -1,12 +1,10 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. +// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. -namespace Bug3758 -{ - open Microsoft.Quantum.Primitive; +namespace Bug3758 { + open Microsoft.Quantum.Intrinsic; - function Reverse<'T> (array : 'T[]) : 'T[] - { + function Reverse<'T> (array : 'T[]) : 'T[] { let nElements = Length(array); return array[nElements-1..-1..0]; } @@ -22,43 +20,31 @@ namespace Bug3758 /// # Summary /// An example operation used for testing input transformations. - operation TransformationReferenceForward(register : Qubit[]) : Unit { - body (...) { - X(register[0]); - H(register[1]); - X(register[2]); - } - adjoint auto; - controlled auto; - controlled adjoint auto; + operation TransformationReferenceForward(register : Qubit[]) : Unit is Adj + Ctl { + X(register[0]); + H(register[1]); + X(register[2]); } /// # Summary /// An example operation used for testing input transformations. - operation TransformationReferenceReverse(register : Qubit[]) : Unit { - body (...) { - X(register[2]); - H(register[1]); - X(register[0]); - } - adjoint auto; - controlled auto; - controlled adjoint auto; + operation TransformationReferenceReverse(register : Qubit[]) : Unit is Adj + Ctl { + X(register[2]); + H(register[1]); + X(register[0]); } } -namespace Microsoft.Quantum.Simulation.Simulators.Tests.Circuits -{ +namespace Microsoft.Quantum.Simulation.Simulators.Tests.Circuits { open Bug3758; - open Microsoft.Quantum.Extensions.Testing; + open Microsoft.Quantum.Diagnostics; operation TransformedOperationTest() : Unit { - AssertOperationsEqualReferenced( + AssertOperationsEqualReferenced(3, TransformedOperation(Reverse, TransformationReferenceReverse), - TransformationReferenceForward, - 3 + TransformationReferenceForward ); } } From c80fad44e9796a188af6d7133c27f35050f73f0a Mon Sep 17 00:00:00 2001 From: Christopher Granade Date: Wed, 24 Jun 2020 10:50:22 -0700 Subject: [PATCH 5/8] Move IndexRange as well. --- .../QsharpCore/Arrays/Enumeration.qs | 33 +++++++++++++++++++ .../QsharpCore/Diagnostics/Assert.qs | 3 ++ 2 files changed, 36 insertions(+) create mode 100644 src/Simulation/QsharpCore/Arrays/Enumeration.qs diff --git a/src/Simulation/QsharpCore/Arrays/Enumeration.qs b/src/Simulation/QsharpCore/Arrays/Enumeration.qs new file mode 100644 index 00000000000..92c2ff7d67b --- /dev/null +++ b/src/Simulation/QsharpCore/Arrays/Enumeration.qs @@ -0,0 +1,33 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +namespace Microsoft.Quantum.Diagnostics { + open Microsoft.Quantum.Intrinsic; + open Microsoft.Quantum.Canon; + + /// # Summary + /// Given an array, returns a range over the indices of that array, suitable + /// for use in a for loop. + /// + /// # Type Parameters + /// ## 'TElement + /// The type of elements of the array. + /// + /// # Input + /// ## array + /// An array for which a range of indices should be returned. + /// + /// # Output + /// A range over all indices of the array. + /// + /// # Example + /// The following `for` loops are equivalent: + /// ```Q# + /// for (idx in IndexRange(array)) { ... } + /// for (idx in IndexRange(array)) { ... } + /// ``` + function IndexRange<'TElement>(array : 'TElement[]) : Range { + return 0..(Length(array) - 1); + } + +} diff --git a/src/Simulation/QsharpCore/Diagnostics/Assert.qs b/src/Simulation/QsharpCore/Diagnostics/Assert.qs index 08e871a9443..6f5cb339633 100644 --- a/src/Simulation/QsharpCore/Diagnostics/Assert.qs +++ b/src/Simulation/QsharpCore/Diagnostics/Assert.qs @@ -1,3 +1,6 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + namespace Microsoft.Quantum.Diagnostics { open Microsoft.Quantum.Intrinsic; open Microsoft.Quantum.Canon; From 7bd91ba81b9de41f43168121cec382f5fa7b5926 Mon Sep 17 00:00:00 2001 From: Christopher Granade Date: Wed, 24 Jun 2020 11:27:37 -0700 Subject: [PATCH 6/8] Fixed typo in IndexRange namespace. --- src/Simulation/QsharpCore/Arrays/Enumeration.qs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Simulation/QsharpCore/Arrays/Enumeration.qs b/src/Simulation/QsharpCore/Arrays/Enumeration.qs index 92c2ff7d67b..a90383b1ab5 100644 --- a/src/Simulation/QsharpCore/Arrays/Enumeration.qs +++ b/src/Simulation/QsharpCore/Arrays/Enumeration.qs @@ -1,7 +1,7 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. -namespace Microsoft.Quantum.Diagnostics { +namespace Microsoft.Quantum.Arrays { open Microsoft.Quantum.Intrinsic; open Microsoft.Quantum.Canon; From 29e2db5296fbba806e63cf241a3f4f583da749f8 Mon Sep 17 00:00:00 2001 From: Christopher Granade Date: Thu, 25 Jun 2020 10:08:23 -0700 Subject: [PATCH 7/8] Fixed AllocateQubit2 test. --- src/Simulation/Simulators.Tests/Circuits/Fail.qs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Simulation/Simulators.Tests/Circuits/Fail.qs b/src/Simulation/Simulators.Tests/Circuits/Fail.qs index fc6aa685f76..1f079c005eb 100644 --- a/src/Simulation/Simulators.Tests/Circuits/Fail.qs +++ b/src/Simulation/Simulators.Tests/Circuits/Fail.qs @@ -89,9 +89,11 @@ namespace Microsoft.Quantum.Simulation.Simulators.Tests.Circuits { } operation AllocateQubit2 () : Unit { - using( q = Qubit() ) { + using (q = Qubit()) { Microsoft.Quantum.Intrinsic.H(q); - Microsoft.Quantum.Intrinsic.Assert([PauliZ],[q],One,""); + Microsoft.Quantum.Diagnostics.AssertMeasurement( + [PauliZ], [q], One, "" + ); } } -} \ No newline at end of file +} From de2a93bbd31253249121dd9502abd3f4fd4ace52 Mon Sep 17 00:00:00 2001 From: Christopher Granade Date: Thu, 25 Jun 2020 12:38:28 -0700 Subject: [PATCH 8/8] Fix AllocateQubit2Test. --- .../Simulators.Tests/StackTraceTests.cs | 47 ++++++++++--------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/src/Simulation/Simulators.Tests/StackTraceTests.cs b/src/Simulation/Simulators.Tests/StackTraceTests.cs index 48c142d7e2b..c3fead7b586 100644 --- a/src/Simulation/Simulators.Tests/StackTraceTests.cs +++ b/src/Simulation/Simulators.Tests/StackTraceTests.cs @@ -1,6 +1,8 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. +// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. +#nullable enable + using Xunit; using System; @@ -29,32 +31,33 @@ public StackTraceTests(ITestOutputHelper output) [Fact] public void AllocateQubit2Test() { - using (var sim = new QuantumSimulator()) + using var sim = new QuantumSimulator(); + try { - try - { - IgnorableAssert.Disable(); - QVoid res = sim.Execute(QVoid.Instance); - } - catch (ExecutionFailException) - { - StackFrame[] stackFrames = sim.CallStack; + IgnorableAssert.Disable(); + QVoid res = sim.Execute(QVoid.Instance); + } + catch (ExecutionFailException) + { + var stackFrames = sim.CallStack; - // The following assumes that Assert is on Q# stack. - Assert.Equal(2, stackFrames.Length); + // Make sure that the call stack isn't null before proceeding. + Assert.NotNull(stackFrames); - Assert.Equal("Microsoft.Quantum.Intrinsic.Assert", stackFrames[0].Callable.FullName); - Assert.Equal(namespacePrefix + "AllocateQubit2", stackFrames[1].Callable.FullName); + // The following assumes that Assert is on Q# stack. + Assert.Equal(2, stackFrames!.Length); - Assert.Equal(OperationFunctor.Body, stackFrames[0].Callable.Variant); - Assert.Equal(OperationFunctor.Body, stackFrames[1].Callable.Variant); + Assert.Equal("Microsoft.Quantum.Diagnostics.AssertMeasurement", stackFrames[0].Callable.FullName); + Assert.Equal(namespacePrefix + "AllocateQubit2", stackFrames[1].Callable.FullName); - Assert.Equal(94, stackFrames[1].FailedLineNumber); - } - finally - { - IgnorableAssert.Enable(); - } + Assert.Equal(OperationFunctor.Body, stackFrames[0].Callable.Variant); + Assert.Equal(OperationFunctor.Body, stackFrames[1].Callable.Variant); + + Assert.Equal(94, stackFrames[1].FailedLineNumber); + } + finally + { + IgnorableAssert.Enable(); } }