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

Commit

Permalink
ApplyWindowed (#603)
Browse files Browse the repository at this point in the history
* 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 <michaylova@gmail.com>

Co-authored-by: Mathias Soeken <mathias.soeken@microsoft.com>
Co-authored-by: Mariia Mykhailova <michaylova@gmail.com>
  • Loading branch information
3 people committed Jul 20, 2022
1 parent 131a8e7 commit 0ac7ac1
Showing 1 changed file with 90 additions and 0 deletions.
90 changes: 90 additions & 0 deletions Standard/src/Arrays/Windows.qs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

namespace Microsoft.Quantum.Arrays {
open Microsoft.Quantum.Canon;

/// # Summary
/// Returns all consecutive subarrays of length `size`.
Expand Down Expand Up @@ -73,4 +74,93 @@ namespace Microsoft.Quantum.Arrays {
internal function Prefix<'T>(to : Int, array : 'T[]) : 'T[] {
return array[0..to];
}

/// # Summary
/// Applies an operation windowing over an input register.
///
/// # Input
/// ## windowLen
/// The size of each window.
/// ## op
/// An operation on a register that will be provided with the current window and its index.
/// ## register
/// The register the operation windows over.
///
/// # Example
/// The example below shows how to use `ApplyToEachWindow` to construct a parity function
/// ```qsharp
/// operation Parity(qubits : Qubit[], target : Qubit) : Unit {
/// ApplyToEachWindow(1, (_, q) => CNOT(q[0], target), qubits);
/// }
/// ```
///
/// # Type Parameters
/// ## 'T
/// The type of register elements.
operation ApplyToEachWindow<'T>(windowLen : Int, op : (Int, 'T[]) => Unit, register : 'T[]) : Unit {
ApplyToEach(op, Enumerated(Windows(windowLen, register)));
}

/// # Summary
/// Applies an operation windowing over an input register. The modifier `A` indicates that the single-qubit operation is adjointable.
///
/// # Input
/// ## windowLen
/// The size of each window.
/// ## op
/// An operation on a register that will be provided with the current window and its index.
/// ## register
/// The register the operation windows over.
////
/// # Type Parameters
/// ## 'T
/// The type of register elements.
///
/// # See Also
/// - Microsoft.Quantum.Arrays.ApplyToEachWindow
operation ApplyToEachWindowA<'T>(windowLen : Int, op : (Int, 'T[]) => Unit is Adj, register : 'T[]) : Unit is Adj {
ApplyToEachA(op, Enumerated(Windows(windowLen, register)));
}

/// # Summary
/// Applies an operation windowing over an input register. The modifier `C` indicates that the single-qubit operation is controllable.
///
/// # Input
/// ## windowLen
/// The size of each window.
/// ## op
/// An operation on a register that will be provided with the current window and its index.
/// ## register
/// The register the operation windows over.
////
/// # Type Parameters
/// ## 'T
/// The type of register elements.
///
/// # See Also
/// - Microsoft.Quantum.Arrays.ApplyToEachWindow
operation ApplyToEachWindowC<'T>(windowLen : Int, op : (Int, 'T[]) => Unit is Ctl, register : 'T[]) : Unit is Ctl {
ApplyToEachC(op, Enumerated(Windows(windowLen, register)));
}

/// # Summary
/// Applies an operation windowing over an input register. The modifier `CA` indicates that the single-qubit operation is controllable and adjointable.
///
/// # Input
/// ## windowLen
/// The size of each window.
/// ## op
/// An operation on a register that will be provided with the current window and its index.
/// ## register
/// The register the operation windows over.
////
/// # Type Parameters
/// ## 'T
/// The type of register elements.
///
/// # See Also
/// - Microsoft.Quantum.Arrays.ApplyToEachWindow
operation ApplyToEachWindowCA<'T>(windowLen : Int, op : (Int, 'T[]) => Unit is Adj + Ctl, register : 'T[]) : Unit is Adj + Ctl {
ApplyToEachCA(op, Enumerated(Windows(windowLen, register)));
}
}

0 comments on commit 0ac7ac1

Please sign in to comment.