This repository was archived by the owner on Jan 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathRegisters.qs
More file actions
115 lines (105 loc) · 3.75 KB
/
Registers.qs
File metadata and controls
115 lines (105 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
namespace Microsoft.Quantum.Measurement {
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Canon;
open Microsoft.Quantum.Arrays;
/// # Summary
/// Measures the given Pauli operator using an explicit scratch
/// qubit to perform the measurement.
///
/// # Input
/// ## pauli
/// A multi-qubit Pauli operator specified as an array of
/// single-qubit Pauli operators.
/// ## target
/// Qubit register to be measured.
///
/// # Output
/// The result of measuring the given Pauli operator on
/// the `target` register.
operation MeasureWithScratch (pauli : Pauli[], target : Qubit[]) : Result {
using (scratch = Qubit()) {
H(scratch);
for (idxPauli in IndexRange(pauli)) {
let P = pauli[idxPauli];
let src = target[idxPauli];
if (P == PauliX) {
Controlled X([scratch], src);
} elif (P == PauliY) {
Controlled Y([scratch], src);
} elif (P == PauliZ) {
Controlled Z([scratch], src);
}
}
H(scratch);
return MResetZ(scratch);
}
}
/// # Summary
/// Given an array of multi-qubit Pauli operators, measures each using a specified measurement
/// gadget, then returns the array of results.
///
/// # Input
/// ## paulis
/// Array of multi-qubit Pauli operators to measure.
/// ## target
/// Register on which to measure the given operators.
/// ## gadget
/// Operation which performs the measurement of a given multi-qubit operator.
///
/// # Output
/// The array of results obtained from measuring each element of `paulis`
/// on `target`.
operation MeasurePaulis (paulis : Pauli[][], target : Qubit[], gadget : ((Pauli[], Qubit[]) => Result)) : Result[] {
return ForEach(gadget(_, target), paulis);
}
/// # Summary
/// Measures each qubit in a given array in the standard basis.
/// # Input
/// ## targets
/// An array of qubits to be measured.
/// # Output
/// An array of measurement results.
operation MultiM (targets : Qubit[]) : Result[] {
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.
///
/// # Description
/// Measures a register of qubits in the $Z \otimes Z \otimes \cdots \otimes Z$
/// basis, representing the parity of the entire register.
///
/// # Input
/// ## register
/// The register to be measured.
///
/// # Output
/// The result of measuring $Z \otimes Z \otimes \cdots \otimes Z$.
operation MeasureAllZ (register : Qubit[]) : Result {
return Measure(ConstantArray(Length(register), PauliZ), register);
}
}