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 all commits
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
60 changes: 60 additions & 0 deletions Standard/src/Canon/Combinators/SinglyControlled.qs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// 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 (ctrl, originalInput) => Controlled op([ctrl], originalInput);
}

/// # 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 (ctrl, originalInput) => Controlled op([ctrl], originalInput);
}

}
36 changes: 36 additions & 0 deletions Standard/tests/Canon/Combinators/SinglyControlled.qs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 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 TestSinglyControlledWithSimulation() : Unit {
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");
}
}
}
}

@Test("QuantumSimulator")
operation TestSinglyControlledWithEquivalenceCheck() : Unit {
AssertOperationsEqualReferenced(2,
qs => SinglyControlled(H)(qs[0], qs[1]),
qs => Controlled H([qs[0]], qs[1])
);
}
}