Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

Add new MeasureIfAllZeros operation. #204

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions Standard/src/Measurement/Registers.qs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This behavior differs from that of MeasureInteger - any specific reason why? It seems more neat to be able to write "return MeasureIfAllZeros(register)" than to measure into a variable, then call ResetAll, then return that variable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My intent was to match the semantics of other operations in the Microsoft.Quantum.Measurement namespace; the operations in Microsoft.Quantum.Arithmetic don't follow the same convention (they should be reconciled, but that's a slightly separate discussion).

For the usecase of returning after, I could imagine something like ResetAfter<'T>(op : (Qubit[] => 'T), target : Qubit[]) : 'T { let returnValue = op(target); ResetAll(target); return returnValue; } such that you could do return ResetAfter(MeasureIfAllZeros(register));. That would be less efficient in terms of the number of measurements than return All(ForEach(MResetZ, target)), though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the last code snippet, did you mean something like All(IsResultZero, ForEach(MResetZ, target))?

From the end user point of view, it might be still more convenient to stick with MeasureInteger than to use MeasureIfAllZeros with variable assignment or to chain together several operations from different namespaces...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, thank you; that's the danger of writing code quickly on the way to a meeting, I suppose. Anyway, I think there's two distinct issues that are getting a bit conflated here:

  • Existing measurement operations expose different reset semantics based on whether they are in Microsoft.Quantum.Measurement or Microsoft.Quantum.Arithmetic.
  • Both kinds of reset semantics have independent usecases, and should be made available to uesrs.

/// 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.
///
Expand Down
22 changes: 22 additions & 0 deletions Standard/tests/Measurement/RegisterTests.qs
Original file line number Diff line number Diff line change
@@ -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);
}
}

}