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

Commit 0ac7ac1

Browse files
adrianlehmsoekentcNickolas
authored
ApplyWindowed (#603)
* Add ApplyWindowed operation Applies an operation windowing over input but with const target qubits * Simplify ApplyWindowed * Update ApplyWindowed to be more idiomatic See comment on #602 by @cgranade * Fix documentation style issues for ApplyWindowed * Improve ApplyWindowed documentation and add example * Change name of ApplyWindowed to ApplyToEachWindow * Fix example being in code block Co-authored-by: Mariia Mykhailova <[email protected]> Co-authored-by: Mathias Soeken <[email protected]> Co-authored-by: Mariia Mykhailova <[email protected]>
1 parent 131a8e7 commit 0ac7ac1

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

Standard/src/Arrays/Windows.qs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT License.
33

44
namespace Microsoft.Quantum.Arrays {
5+
open Microsoft.Quantum.Canon;
56

67
/// # Summary
78
/// Returns all consecutive subarrays of length `size`.
@@ -73,4 +74,93 @@ namespace Microsoft.Quantum.Arrays {
7374
internal function Prefix<'T>(to : Int, array : 'T[]) : 'T[] {
7475
return array[0..to];
7576
}
77+
78+
/// # Summary
79+
/// Applies an operation windowing over an input register.
80+
///
81+
/// # Input
82+
/// ## windowLen
83+
/// The size of each window.
84+
/// ## op
85+
/// An operation on a register that will be provided with the current window and its index.
86+
/// ## register
87+
/// The register the operation windows over.
88+
///
89+
/// # Example
90+
/// The example below shows how to use `ApplyToEachWindow` to construct a parity function
91+
/// ```qsharp
92+
/// operation Parity(qubits : Qubit[], target : Qubit) : Unit {
93+
/// ApplyToEachWindow(1, (_, q) => CNOT(q[0], target), qubits);
94+
/// }
95+
/// ```
96+
///
97+
/// # Type Parameters
98+
/// ## 'T
99+
/// The type of register elements.
100+
operation ApplyToEachWindow<'T>(windowLen : Int, op : (Int, 'T[]) => Unit, register : 'T[]) : Unit {
101+
ApplyToEach(op, Enumerated(Windows(windowLen, register)));
102+
}
103+
104+
/// # Summary
105+
/// Applies an operation windowing over an input register. The modifier `A` indicates that the single-qubit operation is adjointable.
106+
///
107+
/// # Input
108+
/// ## windowLen
109+
/// The size of each window.
110+
/// ## op
111+
/// An operation on a register that will be provided with the current window and its index.
112+
/// ## register
113+
/// The register the operation windows over.
114+
////
115+
/// # Type Parameters
116+
/// ## 'T
117+
/// The type of register elements.
118+
///
119+
/// # See Also
120+
/// - Microsoft.Quantum.Arrays.ApplyToEachWindow
121+
operation ApplyToEachWindowA<'T>(windowLen : Int, op : (Int, 'T[]) => Unit is Adj, register : 'T[]) : Unit is Adj {
122+
ApplyToEachA(op, Enumerated(Windows(windowLen, register)));
123+
}
124+
125+
/// # Summary
126+
/// Applies an operation windowing over an input register. The modifier `C` indicates that the single-qubit operation is controllable.
127+
///
128+
/// # Input
129+
/// ## windowLen
130+
/// The size of each window.
131+
/// ## op
132+
/// An operation on a register that will be provided with the current window and its index.
133+
/// ## register
134+
/// The register the operation windows over.
135+
////
136+
/// # Type Parameters
137+
/// ## 'T
138+
/// The type of register elements.
139+
///
140+
/// # See Also
141+
/// - Microsoft.Quantum.Arrays.ApplyToEachWindow
142+
operation ApplyToEachWindowC<'T>(windowLen : Int, op : (Int, 'T[]) => Unit is Ctl, register : 'T[]) : Unit is Ctl {
143+
ApplyToEachC(op, Enumerated(Windows(windowLen, register)));
144+
}
145+
146+
/// # Summary
147+
/// Applies an operation windowing over an input register. The modifier `CA` indicates that the single-qubit operation is controllable and adjointable.
148+
///
149+
/// # Input
150+
/// ## windowLen
151+
/// The size of each window.
152+
/// ## op
153+
/// An operation on a register that will be provided with the current window and its index.
154+
/// ## register
155+
/// The register the operation windows over.
156+
////
157+
/// # Type Parameters
158+
/// ## 'T
159+
/// The type of register elements.
160+
///
161+
/// # See Also
162+
/// - Microsoft.Quantum.Arrays.ApplyToEachWindow
163+
operation ApplyToEachWindowCA<'T>(windowLen : Int, op : (Int, 'T[]) => Unit is Adj + Ctl, register : 'T[]) : Unit is Adj + Ctl {
164+
ApplyToEachCA(op, Enumerated(Windows(windowLen, register)));
165+
}
76166
}

0 commit comments

Comments
 (0)