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
Add repeated operation #153
Merged
cgranade
merged 25 commits into
microsoft:master
from
christopherkang:add_repeated_operation
Nov 22, 2019
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
9d10dec
Drafted new apply ops
christopherkang 2e61df9
Fixed docs
christopherkang f76cf3e
Fixed minor bugs
christopherkang 4efe0be
Merge branch 'master' into add_repeated_operation
ba39745
Added Permutation function + helper Arrays + Claim functions
christopherkang e2282b8
Added Adj + Ctl and set csproj back to generating docs
christopherkang 8580eee
Added tests
christopherkang 09f5435
Fixed test errors
christopherkang 72c76bf
Updated code from comments; moved PermuteQubits to CommonGates
christopherkang 9afaa71
Fixed minor bugs
christopherkang f305f58
Apply suggestions from code review
christopherkang 781312d
Added some fixes from changes
christopherkang 1e0d4a0
Merge branch 'master' into add_repeated_operation
christopherkang 0172ccb
Added most recommendations from Mathias
christopherkang d7673ec
Added example to ApplySeriesOfOps
christopherkang b74e211
Added new examples
christopherkang 58236ba
Added PermuteQubits example
christopherkang 68e0da4
Changed Swap Order to be appending to an array, added test for it
christopherkang 53ac666
Updated ApplyOpRepeatedlyOver Docs
christopherkang 47d8838
Merge branch 'master' into add_repeated_operation
christopherkang cf53aca
Added Mathias' comments
christopherkang ec17ec3
Reverted csproj file
christopherkang 4e75b08
Renamed TupleArrayAsNestedArray
christopherkang e74c4d9
Merge branch 'master' into add_repeated_operation
0c11100
Merge branch 'master' into add_repeated_operation
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,256 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| namespace Microsoft.Quantum.Canon { | ||
| open Microsoft.Quantum.Diagnostics; | ||
| open Microsoft.Quantum.Arrays; | ||
| open Microsoft.Quantum.Intrinsic; | ||
|
|
||
| /////////////////////////////////////////////////////////////////////////////////////////////// | ||
| // Helpers to repeatedly apply operations over qubit arrays | ||
| /////////////////////////////////////////////////////////////////////////////////////////////// | ||
|
|
||
| /// # Summary | ||
| /// Applies a list of ops and their targets sequentially on an array. | ||
| /// | ||
| /// # Input | ||
| /// ## listOfOps | ||
| /// List of ops, each taking a 'T array, to be applied. They are applied sequentially, lowest index first. | ||
| /// ## targets | ||
| /// Nested arrays describing the targets of the op. Each array should contain a list of ints describing | ||
| /// the indices to be used. | ||
| /// ## register | ||
| /// Qubit register to be acted upon. | ||
| /// | ||
| /// ## Example | ||
| /// // The following applies Exp([PauliX, PauliY], 0.5) to qubits 0, 1 | ||
| /// // then X to qubit 2 | ||
| /// let ops = [Exp([PauliX, PauliY], 0.5, _), ApplyToFirstQubit(X, _)]; | ||
| /// let indices = [[0, 1], [2]]; | ||
| /// ApplySeriesOfOps(ops, indices, qubitArray); | ||
| /// | ||
| /// # See Also | ||
| /// - Microsoft.Quantum.Canon.ApplyOpRepeatedlyOver | ||
christopherkang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| operation ApplySeriesOfOps<'T>(listOfOps : ('T[] => Unit)[], targets : Int[][], register : 'T[]) : Unit { | ||
| if (Length(listOfOps) != Length(targets)) { | ||
| fail "The number of ops and number of targets do not match!"; | ||
| } | ||
| for ((op, targetIndices) in Zip(listOfOps, targets)) { | ||
| if (Length(targetIndices) > Length(register)) { | ||
| fail "There are too many targets!"; | ||
| } | ||
| op(Subarray(targetIndices, register)); | ||
| } | ||
| } | ||
|
|
||
| /// # Summary | ||
| /// Applies a list of ops and their targets sequentially on an array. (Adjoint) | ||
| /// | ||
| /// # Input | ||
| /// ## listOfOps | ||
| /// List of ops, each taking a 'T array, to be applied. They are applied sequentially, lowest index first. | ||
| /// Each must have an adjoint functor | ||
| /// ## targets | ||
| /// Nested arrays describing the targets of the op. Each array should contain a list of ints describing | ||
| /// the indices to be used. | ||
| /// ## register | ||
| /// Qubit register to be acted upon. | ||
| /// | ||
| /// ## Example | ||
| /// // The following applies Exp([PauliX, PauliY], 0.5) to qubits 0, 1 | ||
| /// // then X to qubit 2 | ||
| /// let ops = [Exp([PauliX, PauliY], 0.5, _), ApplyToFirstQubitA(X, _)]; | ||
| /// let indices = [[0, 1], [2]]; | ||
| /// ApplySeriesOfOpsA(ops, indices, qubitArray); | ||
| /// | ||
| /// # See Also | ||
| /// - Microsoft.Quantum.Canon.ApplyOpRepeatedlyOver | ||
| operation ApplySeriesOfOpsA<'T>(listOfOps : ('T[] => Unit is Adj)[], targets : Int[][], register : 'T[]) : Unit is Adj{ | ||
| if (Length(listOfOps) != Length(targets)) { | ||
| fail "The number of ops and number of targets do not match!"; | ||
| } | ||
| for ((op, targetIndices) in Zip(listOfOps, targets)) { | ||
| if (Length(targetIndices) > Length(register)) { | ||
| fail "There are too many targets!"; | ||
| } | ||
| op(Subarray(targetIndices, register)); | ||
| } | ||
| } | ||
|
|
||
| /// # Summary | ||
| /// Applies a list of ops and their targets sequentially on an array. (Controlled) | ||
| /// | ||
| /// # Input | ||
| /// ## listOfOps | ||
| /// List of ops, each taking a 'T array, to be applied. They are applied sequentially, lowest index first. | ||
| /// Each must have a Controlled functor | ||
| /// ## targets | ||
| /// Nested arrays describing the targets of the op. Each array should contain a list of ints describing | ||
| /// the indices to be used. | ||
| /// ## register | ||
| /// Qubit register to be acted upon. | ||
| /// | ||
| /// ## Example | ||
| /// // The following applies Exp([PauliX, PauliY], 0.5) to qubits 0, 1 | ||
| /// // then X to qubit 2 | ||
| /// let ops = [Exp([PauliX, PauliY], 0.5, _), ApplyToFirstQubitC(X, _)]; | ||
| /// let indices = [[0, 1], [2]]; | ||
| /// ApplySeriesOfOpsC(ops, indices, qubitArray); | ||
| /// | ||
| /// # See Also | ||
| /// - Microsoft.Quantum.Canon.ApplyOpRepeatedlyOver | ||
| operation ApplySeriesOfOpsC<'T>(listOfOps : ('T[] => Unit is Ctl)[], targets : Int[][], register : 'T[]) : Unit is Ctl{ | ||
| if (Length(listOfOps) != Length(targets)) { | ||
| fail "The number of ops and number of targets do not match!"; | ||
| } | ||
| for ((op, targetIndices) in Zip(listOfOps, targets)) { | ||
| if (Length(targetIndices) > Length(register)) { | ||
| fail "There are too many targets!"; | ||
| } | ||
| op(Subarray(targetIndices, register)); | ||
| } | ||
| } | ||
|
|
||
| /// # Summary | ||
| /// Applies a list of ops and their targets sequentially on an array. (Adjoint + Controlled) | ||
| /// | ||
| /// # Input | ||
| /// ## listOfOps | ||
| /// List of ops, each taking a 'T array, to be applied. They are applied sequentially, lowest index first. | ||
| /// Each must have both an Adjoint and Controlled functor. | ||
| /// ## targets | ||
| /// Nested arrays describing the targets of the op. Each array should contain a list of ints describing | ||
| /// the indices to be used. | ||
| /// ## register | ||
| /// Qubit register to be acted upon. | ||
| /// | ||
| /// ## Example | ||
| /// // The following applies Exp([PauliX, PauliY], 0.5) to qubits 0, 1 | ||
| /// // then X to qubit 2 | ||
| /// let ops = [Exp([PauliX, PauliY], 0.5, _), ApplyToFirstQubitCA(X, _)]; | ||
| /// let indices = [[0, 1], [2]]; | ||
| /// ApplySeriesOfOpsCA(ops, indices, qubitArray); | ||
| /// | ||
| /// # See Also | ||
| /// - Microsoft.Quantum.Canon.ApplyOpRepeatedlyOver | ||
| operation ApplySeriesOfOpsCA<'T>(listOfOps : ('T[] => Unit is Adj + Ctl)[], targets : Int[][], register : 'T[]) : Unit is Adj + Ctl{ | ||
| if (Length(listOfOps) != Length(targets)) { | ||
| fail "The number of ops and number of targets do not match!"; | ||
| } | ||
| for ((op, targetIndices) in Zip(listOfOps, targets)) { | ||
| if (Length(targetIndices) > Length(register)) { | ||
| fail "There are too many targets!"; | ||
| } | ||
| op(Subarray(targetIndices, register)); | ||
| } | ||
| } | ||
|
|
||
| /// # Summary | ||
| /// Applies the same op over a qubit register multiple times. | ||
| /// | ||
| /// # Input | ||
| /// ## op | ||
| /// An operation to be applied multiple times on the qubit register | ||
| /// ## targets | ||
| /// Nested arrays describing the targets of the op. Each array should contain a list of ints describing | ||
| /// the qubits to be used. | ||
| /// ## register | ||
| /// Qubit register to be acted upon. | ||
| /// | ||
| /// # See Also | ||
| /// - Microsoft.Quantum.Canon.ApplySeriesOfOps | ||
| operation ApplyOpRepeatedlyOver(op : (Qubit[] => Unit), targets : Int[][], register : Qubit[]) : Unit | ||
| { | ||
| for (target in targets) | ||
| { | ||
| if (Length(target) > Length(register)) | ||
| { | ||
| fail "Too many targets!"; | ||
| } | ||
| let opTargets = Subarray(target, register); | ||
| op(opTargets); | ||
| } | ||
| } | ||
|
|
||
| /// # Summary | ||
| /// Applies the same op over a qubit register multiple times. | ||
| /// | ||
| /// # Input | ||
| /// ## op | ||
| /// An operation to be applied multiple times on the qubit register | ||
| /// ## targets | ||
| /// Nested arrays describing the targets of the op. Each array should contain a list of ints describing | ||
| /// the qubits to be used. | ||
| /// ## register | ||
| /// Qubit register to be acted upon. | ||
| /// | ||
| /// # See Also | ||
| /// - Microsoft.Quantum.Canon.ApplySeriesOfOps | ||
| operation ApplyOpRepeatedlyOverA(op : (Qubit[] => Unit is Adj), targets : Int[][], register : Qubit[]) : Unit is Adj | ||
| { | ||
| for (target in targets) | ||
| { | ||
| if (Length(target) > Length(register)) | ||
| { | ||
| fail "Too many targets!"; | ||
| } | ||
| let opTargets = Subarray(target, register); | ||
| op(opTargets); | ||
| } | ||
| } | ||
|
|
||
| /// # Summary | ||
| /// Applies the same op over a qubit register multiple times. | ||
| /// | ||
| /// # Input | ||
| /// ## op | ||
| /// An operation to be applied multiple times on the qubit register | ||
| /// ## targets | ||
| /// Nested arrays describing the targets of the op. Each array should contain a list of ints describing | ||
| /// the qubits to be used. | ||
| /// ## register | ||
| /// Qubit register to be acted upon. | ||
| /// | ||
| /// # See Also | ||
| /// - Microsoft.Quantum.Canon.ApplySeriesOfOps | ||
| operation ApplyOpRepeatedlyOverC(op : (Qubit[] => Unit is Ctl), targets : Int[][], register : Qubit[]) : Unit is Ctl | ||
| { | ||
| for (target in targets) | ||
| { | ||
| if (Length(target) > Length(register)) | ||
| { | ||
| fail "Too many targets!"; | ||
| } | ||
| let opTargets = Subarray(target, register); | ||
| op(opTargets); | ||
| } | ||
| } | ||
|
|
||
| /// # Summary | ||
| /// Applies the same op over a qubit register multiple times. | ||
| /// | ||
| /// # Input | ||
| /// ## op | ||
| /// An operation to be applied multiple times on the qubit register | ||
| /// ## targets | ||
| /// Nested arrays describing the targets of the op. Each array should contain a list of ints describing | ||
| /// the qubits to be used. | ||
| /// ## register | ||
| /// Qubit register to be acted upon. | ||
| /// | ||
| /// # See Also | ||
| /// - Microsoft.Quantum.Canon.ApplySeriesOfOps | ||
| operation ApplyOpRepeatedlyOverCA(op : (Qubit[] => Unit is Adj+Ctl), targets : Int[][], register : Qubit[]) : Unit is Adj+Ctl | ||
| { | ||
| for (target in targets) | ||
| { | ||
| if (Length(target) > Length(register)) | ||
| { | ||
| fail "Too many targets!"; | ||
| } | ||
| let opTargets = Subarray(target, register); | ||
| op(opTargets); | ||
| } | ||
| } | ||
|
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.