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

Commit c246868

Browse files
authored
Implements #442 (#552)
* Implements #442. * Address reviewer feedback.
1 parent 9ea44b5 commit c246868

2 files changed

Lines changed: 96 additions & 0 deletions

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
namespace Microsoft.Quantum.Canon {
5+
6+
/// # Summary
7+
/// Given a controllable operation, returns a controlled version of that operation
8+
/// accepting exactly one control qubit.
9+
///
10+
/// # Input
11+
/// ## op
12+
/// The operation to be controlled.
13+
///
14+
/// # Output
15+
/// A controlled variant of `op` accepting exactly one control qubit.
16+
///
17+
/// # Example
18+
/// To add the weight (number of "1" bits) of a control register to
19+
/// a target register:
20+
/// ```qsharp
21+
/// ApplyToEachCA(
22+
/// SinglyControlled(IncrementByInteger)(_, (1, target)),
23+
/// controls)
24+
/// );
25+
/// ```
26+
///
27+
/// # See Also
28+
/// - Microsoft.Quantum.Canon.SinglyControlledA
29+
function SinglyControlled<'T>(op : 'T => Unit is Ctl) : (Qubit, 'T) => Unit is Ctl {
30+
return (ctrl, originalInput) => Controlled op([ctrl], originalInput);
31+
}
32+
33+
/// # Summary
34+
/// Given a controllable operation, returns a controlled version of that operation
35+
/// accepting exactly one control qubit.
36+
///
37+
/// # Input
38+
/// ## op
39+
/// The operation to be controlled.
40+
///
41+
/// # Output
42+
/// A controlled variant of `op` accepting exactly one control qubit.
43+
///
44+
/// # Example
45+
/// To add the weight (number of "1" bits) of a control register to
46+
/// a target register:
47+
/// ```qsharp
48+
/// ApplyToEachCA(
49+
/// SinglyControlledA(IncrementByInteger)(_, (1, target)),
50+
/// controls)
51+
/// );
52+
/// ```
53+
///
54+
/// # See Also
55+
/// - Microsoft.Quantum.Canon.SinglyControlled
56+
function SinglyControlledA<'T>(op : 'T => Unit is Adj + Ctl) : (Qubit, 'T) => Unit is Adj + Ctl {
57+
return (ctrl, originalInput) => Controlled op([ctrl], originalInput);
58+
}
59+
60+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
namespace Microsoft.Quantum.Tests {
5+
open Microsoft.Quantum.Arithmetic;
6+
open Microsoft.Quantum.Canon;
7+
open Microsoft.Quantum.Diagnostics;
8+
open Microsoft.Quantum.Intrinsic;
9+
10+
@Test("ToffoliSimulator")
11+
operation TestSinglyControlledWithSimulation() : Unit {
12+
use ctl = Qubit();
13+
use target = Qubit[3];
14+
let targetLE = LittleEndian(target);
15+
let value = 5;
16+
17+
for singlyControlled in [SinglyControlled, SinglyControlledA] {
18+
for enableControl in [false, true] {
19+
within {
20+
ApplyIfA(enableControl, X, ctl);
21+
} apply {
22+
singlyControlled(ApplyXorInPlace(value, _))(ctl, targetLE);
23+
EqualityFactI(MeasureInteger(targetLE), enableControl ? value | 0, "Unexpected measurement result for SinglyControlled");
24+
}
25+
}
26+
}
27+
}
28+
29+
@Test("QuantumSimulator")
30+
operation TestSinglyControlledWithEquivalenceCheck() : Unit {
31+
AssertOperationsEqualReferenced(2,
32+
qs => SinglyControlled(H)(qs[0], qs[1]),
33+
qs => Controlled H([qs[0]], qs[1])
34+
);
35+
}
36+
}

0 commit comments

Comments
 (0)