Skip to content
This repository was archived by the owner on Jan 12, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions Standard/src/Canon/Combinators/SinglyControlled.qs
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, _, _);
Copy link
Copy Markdown
Contributor

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:

Suggested change
return ApplySinglyControlled(op, _, _);
return (ctrl, originalInput) => Controlled op([ctrl], originalInput);

Not sure if that generates the right characteristics, as I've not tested that yet, sorry.

Copy link
Copy Markdown
Member Author

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.

}

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);
}

}
28 changes: 28 additions & 0 deletions Standard/tests/Canon/Combinators/SinglyControlled.qs
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 {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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 H:

AssertOperationsEqualReferenced(2,
     (qs) => SinglyControlled(H)(qs[0], qs[1]),
     (qs) => Controlled H([qs[0]], qs[1])
);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The 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");
}
}
}
}
}