From 63d6bfd1f3c98e40c5c1645678a3c096adf0f021 Mon Sep 17 00:00:00 2001 From: "Stefan J. Wernli" Date: Mon, 23 Nov 2020 10:51:06 -0800 Subject: [PATCH 1/4] Add Adj + Ctl to Assert methods that support it Fixes Add Adj functor to AssertQubit #316 --- .../QSharpFoundation/Diagnostics/AssertAllZero.qs | 5 +++-- src/Simulation/QSharpFoundation/Diagnostics/AssertQubit.qs | 6 +++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Simulation/QSharpFoundation/Diagnostics/AssertAllZero.qs b/src/Simulation/QSharpFoundation/Diagnostics/AssertAllZero.qs index e9ab9120a34..881b0d4b11f 100644 --- a/src/Simulation/QSharpFoundation/Diagnostics/AssertAllZero.qs +++ b/src/Simulation/QSharpFoundation/Diagnostics/AssertAllZero.qs @@ -14,7 +14,7 @@ namespace Microsoft.Quantum.Diagnostics { /// /// # See Also /// - AssertQubit - operation AssertAllZero (qubits : Qubit[]) : Unit { + operation AssertAllZero (qubits : Qubit[]) : Unit is Adj + Ctl{ body (...) { for qubit in qubits { AssertQubit(Zero, qubit); @@ -39,7 +39,8 @@ namespace Microsoft.Quantum.Diagnostics { /// /// # See Also /// - AssertQubitWithinTolerance - operation AssertAllZeroWithinTolerance(qubits : Qubit[], tolerance : Double) : Unit { + operation AssertAllZeroWithinTolerance(qubits : Qubit[], tolerance : Double) : Unit is Adj + Ctl{ + body (...) { for qubit in qubits { AssertQubitWithinTolerance(Zero, qubit, tolerance); diff --git a/src/Simulation/QSharpFoundation/Diagnostics/AssertQubit.qs b/src/Simulation/QSharpFoundation/Diagnostics/AssertQubit.qs index 04934d1d988..79e1442ce9f 100644 --- a/src/Simulation/QSharpFoundation/Diagnostics/AssertQubit.qs +++ b/src/Simulation/QSharpFoundation/Diagnostics/AssertQubit.qs @@ -21,7 +21,7 @@ namespace Microsoft.Quantum.Diagnostics { /// # Remarks /// allows for asserting /// arbitrary qubit states rather than only $Z$ eigenstates. - operation AssertQubit (expected : Result, q : Qubit) : Unit { + operation AssertQubit (expected : Result, q : Qubit) : Unit is Adj + Ctl { AssertMeasurement([PauliZ], [q], expected, $"Qubit in invalid state. Expecting: {expected}"); } @@ -46,7 +46,7 @@ namespace Microsoft.Quantum.Diagnostics { /// # Remarks /// allows for asserting /// arbitrary qubit states rather than only $Z$ eigenstates. - operation AssertQubitWithinTolerance(expected : Result, q : Qubit, tolerance : Double) : Unit { + operation AssertQubitWithinTolerance(expected : Result, q : Qubit, tolerance : Double) : Unit is Adj + Ctl { AssertMeasurementProbability([PauliZ], [q], expected, 1.0, $"Qubit in invalid state. Expecting: {expected} with tolerance {tolerance}", tolerance); } @@ -101,7 +101,7 @@ namespace Microsoft.Quantum.Diagnostics { /// This is only true under the assumption that Tr(ρ) and Tr(|ψ⟩⟨ψ|) are both 1 (e.g. x₁ = 1/2, y₁ = 1/2). /// If this is not the case, the function asserts that l∞ distance between /// (x₂-x₁,x₃-x₁,x₄-x₁,x₄+x₁) and (y₂-y₁,y₃-y₁,y₄-y₁,y₄+y₁) is less than the tolerance parameter. - operation AssertQubitIsInStateWithinTolerance(expected : (Complex, Complex), register : Qubit, tolerance : Double) : Unit { + operation AssertQubitIsInStateWithinTolerance(expected : (Complex, Complex), register : Qubit, tolerance : Double) : Unit is Adj + Ctl { let (a, b) = expected; let (reA, imA) = a!; let (reB, imB) = b!; From 08f5dc77f9401131822b57cff0b74fa69cc28b87 Mon Sep 17 00:00:00 2001 From: "Stefan J. Wernli" Date: Mon, 23 Nov 2020 13:53:20 -0800 Subject: [PATCH 2/4] Don't propagate Ctl we don't want to support long term --- src/Simulation/QSharpFoundation/Diagnostics/AssertQubit.qs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Simulation/QSharpFoundation/Diagnostics/AssertQubit.qs b/src/Simulation/QSharpFoundation/Diagnostics/AssertQubit.qs index 79e1442ce9f..dafe3a00d3f 100644 --- a/src/Simulation/QSharpFoundation/Diagnostics/AssertQubit.qs +++ b/src/Simulation/QSharpFoundation/Diagnostics/AssertQubit.qs @@ -21,7 +21,7 @@ namespace Microsoft.Quantum.Diagnostics { /// # Remarks /// allows for asserting /// arbitrary qubit states rather than only $Z$ eigenstates. - operation AssertQubit (expected : Result, q : Qubit) : Unit is Adj + Ctl { + operation AssertQubit (expected : Result, q : Qubit) : Unit is Adj { AssertMeasurement([PauliZ], [q], expected, $"Qubit in invalid state. Expecting: {expected}"); } @@ -46,7 +46,7 @@ namespace Microsoft.Quantum.Diagnostics { /// # Remarks /// allows for asserting /// arbitrary qubit states rather than only $Z$ eigenstates. - operation AssertQubitWithinTolerance(expected : Result, q : Qubit, tolerance : Double) : Unit is Adj + Ctl { + operation AssertQubitWithinTolerance(expected : Result, q : Qubit, tolerance : Double) : Unit is Adj { AssertMeasurementProbability([PauliZ], [q], expected, 1.0, $"Qubit in invalid state. Expecting: {expected} with tolerance {tolerance}", tolerance); } @@ -101,7 +101,7 @@ namespace Microsoft.Quantum.Diagnostics { /// This is only true under the assumption that Tr(ρ) and Tr(|ψ⟩⟨ψ|) are both 1 (e.g. x₁ = 1/2, y₁ = 1/2). /// If this is not the case, the function asserts that l∞ distance between /// (x₂-x₁,x₃-x₁,x₄-x₁,x₄+x₁) and (y₂-y₁,y₃-y₁,y₄-y₁,y₄+y₁) is less than the tolerance parameter. - operation AssertQubitIsInStateWithinTolerance(expected : (Complex, Complex), register : Qubit, tolerance : Double) : Unit is Adj + Ctl { + operation AssertQubitIsInStateWithinTolerance(expected : (Complex, Complex), register : Qubit, tolerance : Double) : Unit is Adj { let (a, b) = expected; let (reA, imA) = a!; let (reB, imB) = b!; From e350b071ebc4cd7c34bd0e8b4a1cc82407542b8b Mon Sep 17 00:00:00 2001 From: "Stefan J. Wernli" Date: Fri, 15 Jan 2021 14:06:16 -0800 Subject: [PATCH 3/4] Fix spacing --- src/Simulation/QSharpFoundation/Diagnostics/AssertAllZero.qs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Simulation/QSharpFoundation/Diagnostics/AssertAllZero.qs b/src/Simulation/QSharpFoundation/Diagnostics/AssertAllZero.qs index 881b0d4b11f..df50f5c7e2e 100644 --- a/src/Simulation/QSharpFoundation/Diagnostics/AssertAllZero.qs +++ b/src/Simulation/QSharpFoundation/Diagnostics/AssertAllZero.qs @@ -14,7 +14,7 @@ namespace Microsoft.Quantum.Diagnostics { /// /// # See Also /// - AssertQubit - operation AssertAllZero (qubits : Qubit[]) : Unit is Adj + Ctl{ + operation AssertAllZero (qubits : Qubit[]) : Unit is Adj + Ctl { body (...) { for qubit in qubits { AssertQubit(Zero, qubit); From 7dc427d43891946ae85a675e6e2770313213f25f Mon Sep 17 00:00:00 2001 From: "Stefan J. Wernli" Date: Tue, 23 Mar 2021 11:02:15 -0700 Subject: [PATCH 4/4] Update doc comments and AssertQubit callables --- .../QSharpFoundation/Diagnostics/AssertAllZero.qs | 9 +++++++++ .../QSharpFoundation/Diagnostics/AssertQubit.qs | 15 ++++++++++++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/Simulation/QSharpFoundation/Diagnostics/AssertAllZero.qs b/src/Simulation/QSharpFoundation/Diagnostics/AssertAllZero.qs index df50f5c7e2e..2511a12486e 100644 --- a/src/Simulation/QSharpFoundation/Diagnostics/AssertAllZero.qs +++ b/src/Simulation/QSharpFoundation/Diagnostics/AssertAllZero.qs @@ -14,6 +14,10 @@ namespace Microsoft.Quantum.Diagnostics { /// /// # See Also /// - AssertQubit + /// + /// # Remarks + /// Note that the Adjoint and Controlled versions of this operation will not + /// check the condition. operation AssertAllZero (qubits : Qubit[]) : Unit is Adj + Ctl { body (...) { for qubit in qubits { @@ -39,6 +43,10 @@ namespace Microsoft.Quantum.Diagnostics { /// /// # See Also /// - AssertQubitWithinTolerance + /// + /// # Remarks + /// Note that the Adjoint and Controlled versions of this operation will not + /// check the condition. operation AssertAllZeroWithinTolerance(qubits : Qubit[], tolerance : Double) : Unit is Adj + Ctl{ body (...) { @@ -52,6 +60,7 @@ namespace Microsoft.Quantum.Diagnostics { controlled (ctrls, ...) { AssertAllZeroWithinTolerance(qubits, tolerance); } + controlled adjoint self; } } diff --git a/src/Simulation/QSharpFoundation/Diagnostics/AssertQubit.qs b/src/Simulation/QSharpFoundation/Diagnostics/AssertQubit.qs index dafe3a00d3f..e1a9e993012 100644 --- a/src/Simulation/QSharpFoundation/Diagnostics/AssertQubit.qs +++ b/src/Simulation/QSharpFoundation/Diagnostics/AssertQubit.qs @@ -21,7 +21,10 @@ namespace Microsoft.Quantum.Diagnostics { /// # Remarks /// allows for asserting /// arbitrary qubit states rather than only $Z$ eigenstates. - operation AssertQubit (expected : Result, q : Qubit) : Unit is Adj { + /// + /// Note that the Adjoint and Controlled versions of this operation will not + /// check the condition. + operation AssertQubit (expected : Result, q : Qubit) : Unit is Adj + Ctl { AssertMeasurement([PauliZ], [q], expected, $"Qubit in invalid state. Expecting: {expected}"); } @@ -46,7 +49,10 @@ namespace Microsoft.Quantum.Diagnostics { /// # Remarks /// allows for asserting /// arbitrary qubit states rather than only $Z$ eigenstates. - operation AssertQubitWithinTolerance(expected : Result, q : Qubit, tolerance : Double) : Unit is Adj { + /// + /// Note that the Adjoint and Controlled versions of this operation will not + /// check the condition. + operation AssertQubitWithinTolerance(expected : Result, q : Qubit, tolerance : Double) : Unit is Adj + Ctl { AssertMeasurementProbability([PauliZ], [q], expected, 1.0, $"Qubit in invalid state. Expecting: {expected} with tolerance {tolerance}", tolerance); } @@ -101,7 +107,10 @@ namespace Microsoft.Quantum.Diagnostics { /// This is only true under the assumption that Tr(ρ) and Tr(|ψ⟩⟨ψ|) are both 1 (e.g. x₁ = 1/2, y₁ = 1/2). /// If this is not the case, the function asserts that l∞ distance between /// (x₂-x₁,x₃-x₁,x₄-x₁,x₄+x₁) and (y₂-y₁,y₃-y₁,y₄-y₁,y₄+y₁) is less than the tolerance parameter. - operation AssertQubitIsInStateWithinTolerance(expected : (Complex, Complex), register : Qubit, tolerance : Double) : Unit is Adj { + /// + /// Note that the Adjoint and Controlled versions of this operation will not + /// check the condition. + operation AssertQubitIsInStateWithinTolerance(expected : (Complex, Complex), register : Qubit, tolerance : Double) : Unit is Adj + Ctl { let (a, b) = expected; let (reA, imA) = a!; let (reB, imB) = b!;