diff --git a/Standard/src/Measurement/Registers.qs b/Standard/src/Measurement/Registers.qs index f5052180bf9..103fa2de36b 100644 --- a/Standard/src/Measurement/Registers.qs +++ b/Standard/src/Measurement/Registers.qs @@ -72,6 +72,29 @@ namespace Microsoft.Quantum.Measurement { return ForEach(M, targets); } + /// # Summary + /// Measures a register of qubits and returns true if it is in the all-zeros state or false otherwise. + /// + /// # Description + /// Given a register of qubits, checks if that register is in the state + /// $\ket{00 \cdots 0}$ by performing a computational basis (i.e.: + /// `PauliZ`) measurement on each individual qubit in the register. + /// + /// # Input + /// ## register + /// The register of qubits to be measured. + /// + /// # Output + /// `true` if and only if the register is measured to be in the + /// $\ket{00 \cdots 0}$ state. + /// + /// # Remarks + /// This operation does not reset its qubits, but projects them to a + /// computational basis state. + operation MeasureIfAllZeros(register : Qubit[]) : Bool { + return All(IsResultZero, ForEach(M, register)); + } + /// # Summary /// Jointly measures a register of qubits in the Pauli Z basis. /// diff --git a/Standard/tests/Measurement/RegisterTests.qs b/Standard/tests/Measurement/RegisterTests.qs new file mode 100644 index 00000000000..f1e472b9778 --- /dev/null +++ b/Standard/tests/Measurement/RegisterTests.qs @@ -0,0 +1,22 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +namespace Microsoft.Quantum.Measurement.Tests { + open Microsoft.Quantum.Intrinsic; + open Microsoft.Quantum.Canon; + open Microsoft.Quantum.Measurement; + open Microsoft.Quantum.Diagnostics; + + @Test("QuantumSimulator") + operation CheckMeasureIfAllZeros() : Unit { + using (qs = Qubit[3]) { + Fact(MeasureIfAllZeros(qs), "MeasureIfAllZeros was false for |000⟩ state."); + + X(qs[1]); + Fact(not MeasureIfAllZeros(qs), "MeasureIfAllZeros was true for |010⟩ state."); + + ResetAll(qs); + } + } + +}