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 181
Implements #442 #552
Merged
Merged
Implements #442 #552
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| namespace Microsoft.Quantum.Canon { | ||
|
|
||
| /// # Summary | ||
| /// Given a controllable operation, returns a controlled version of that operation | ||
| /// accepting exactly one control qubit. | ||
| /// | ||
| /// # Input | ||
| /// ## op | ||
| /// The operation to be controlled. | ||
| /// | ||
| /// # Output | ||
| /// A controlled variant of `op` accepting exactly one control qubit. | ||
| /// | ||
| /// # Example | ||
| /// To add the weight (number of "1" bits) of a control register to | ||
| /// a target register: | ||
| /// ```qsharp | ||
| /// ApplyToEachCA( | ||
| /// SinglyControlled(IncrementByInteger)(_, (1, target)), | ||
| /// controls) | ||
| /// ); | ||
| /// ``` | ||
| /// | ||
| /// # See Also | ||
| /// - Microsoft.Quantum.Canon.SinglyControlledA | ||
| function SinglyControlled<'T>(op : 'T => Unit is Ctl) : (Qubit, 'T) => Unit is Ctl { | ||
| return ApplySinglyControlled(op, _, _); | ||
| } | ||
|
|
||
| internal operation ApplySinglyControlled<'T>(op : 'T => Unit is Ctl, ctl : Qubit, target : 'T) : Unit is Ctl { | ||
| Controlled op([ctl], target); | ||
| } | ||
|
|
||
| /// # Summary | ||
| /// Given a controllable operation, returns a controlled version of that operation | ||
| /// accepting exactly one control qubit. | ||
| /// | ||
| /// # Input | ||
| /// ## op | ||
| /// The operation to be controlled. | ||
| /// | ||
| /// # Output | ||
| /// A controlled variant of `op` accepting exactly one control qubit. | ||
| /// | ||
| /// # Example | ||
| /// To add the weight (number of "1" bits) of a control register to | ||
| /// a target register: | ||
| /// ```qsharp | ||
| /// ApplyToEachCA( | ||
| /// SinglyControlledA(IncrementByInteger)(_, (1, target)), | ||
| /// controls) | ||
| /// ); | ||
| /// ``` | ||
| /// | ||
| /// # See Also | ||
| /// - Microsoft.Quantum.Canon.SinglyControlled | ||
| function SinglyControlledA<'T>(op : 'T => Unit is Adj + Ctl) : (Qubit, 'T) => Unit is Adj + Ctl { | ||
| return ApplySinglyControlledA(op, _, _); | ||
| } | ||
|
|
||
| internal operation ApplySinglyControlledA<'T>(op : 'T => Unit is Adj + Ctl, ctl : Qubit, target : 'T) : Unit is Adj + Ctl { | ||
| Controlled op([ctl], target); | ||
| } | ||
|
|
||
| } | ||
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,28 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| namespace Microsoft.Quantum.Tests { | ||
| open Microsoft.Quantum.Arithmetic; | ||
| open Microsoft.Quantum.Canon; | ||
| open Microsoft.Quantum.Diagnostics; | ||
| open Microsoft.Quantum.Intrinsic; | ||
|
|
||
| @Test("ToffoliSimulator") | ||
| operation TestSinglyControlled() : Unit { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For testing, this is great, but I may also suggest an AssertOperationsEqualReferenced call for something like AssertOperationsEqualReferenced(2,
(qs) => SinglyControlled(H)(qs[0], qs[1]),
(qs) => Controlled H([qs[0]], qs[1])
);
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added this test case. |
||
| use ctl = Qubit(); | ||
| use target = Qubit[3]; | ||
| let targetLE = LittleEndian(target); | ||
| let value = 5; | ||
|
|
||
| for singlyControlled in [SinglyControlled, SinglyControlledA] { | ||
| for enableControl in [false, true] { | ||
| within { | ||
| ApplyIfA(enableControl, X, ctl); | ||
| } apply { | ||
| singlyControlled(ApplyXorInPlace(value, _))(ctl, targetLE); | ||
| EqualityFactI(MeasureInteger(targetLE), enableControl ? value | 0, "Unexpected measurement result for SinglyControlled"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You may be able to write this as a lambda now:
Not sure if that generates the right characteristics, as I've not tested that yet, sorry.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's awesome! And the recent commit confirms it works.