forked from microsoft/Quantum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMultiControlledNOT.qs
166 lines (135 loc) · 6.49 KB
/
MultiControlledNOT.qs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
namespace Microsoft.Quantum.Samples.UnitTesting {
open Microsoft.Quantum.Arrays;
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Canon;
///////////////////////////////////////////////////////////////////////////////////////////////
// Multiply Controlled Not gates
///////////////////////////////////////////////////////////////////////////////////////////////
// This file contains different implementations multiply controlled Not gate,
// also known as multiply controlled Pauli X gate and closely related to
// Multiply Controlled Toffoli gate
// Multiply Controlled Not gate takes a qubit register |c₁,…,cₙ⟩
// with controls and target Qubit |t₁⟩. On computational basis states it acts as:
// |c₁,…,cₙ⟩⊗|t₁⟩ ↦ |c₁,…,cₙ⟩⊗|t₁⊕(c₁∧…∧cₙ)⟩, i.e. the target qubit t is flipped
// if and only if all control qubits are in state |1⟩ .
// The gate is also equivalent to Controlled(Microsoft.Quantum.Intrinsic.X)
///////////////////////////////////////////////////////////////////////////////////////////////
/// # Summary
/// Implements Multiply Controlled Not gate using Microsoft.Quantum.Canon
/// library combinator
///
/// # Input
/// ## controls
/// Quantum register which holds the control qubits
/// ## target
/// Qubit which is the target of the multiply controlled NOT.
///
/// # See Also
/// - @"Microsoft.Quantum.Canon.ApplyMultiControlledCA"
operation MultiControlledXClean (controls : Qubit[], target : Qubit) : Unit {
body (...) {
let numControls = Length(controls);
if (numControls == 0) {
X(target);
}
elif (numControls == 1) {
CNOT(Head(controls), target);
}
elif (numControls == 2) {
CCNOT(controls[1], controls[0], target);
}
else {
let multiNot = ApplyMultiControlledCA(ApplyToFirstThreeQubitsCA(CCNOT, _), CCNOTop(CCNOT), _, _);
multiNot(Rest(controls), [Head(controls), target]);
}
}
adjoint invert;
controlled (extraControls, ...) {
MultiControlledXClean(extraControls + controls, target);
}
controlled adjoint invert;
}
/// # Summary
/// Multiply controlled NOT gate using dirty ancillas, according to Barenco et al
///
/// # Input
/// ## controls
/// Quantum register which holds the control qubits
/// ## target
/// Qubit which is the target of the multiply controlled NOT.
///
/// # References
/// - [ *A. Barenco, Ch.H. Bennett, R. Cleve, D.P. DiVincenzo, N.Margolus, P.Shor,
/// T.Sleator, J.A. Smolin, H. Weinfurter*
/// Phys. Rev. A 52, 3457 (1995)](http://doi.org/10.1103/PhysRevA.52.3457)
///
/// # See Also
/// - For the circuit diagram see the figure on
/// [Page 19 of arXiv:quant-ph/9503016](https://arxiv.org/pdf/quant-ph/9503016v1.pdf#page=19)
/// - File MultiControlledXBorrow.png in the same folder as this file shows
/// the relation between the function implementation and circuit.
///
/// # Remarks
/// The circuit uses (Length(controls)-2) dirty ancillas. These are used as scratch
/// space and are returned in the same state as when they were borrowed.
operation MultiControlledXBorrow (controls : Qubit[], target : Qubit) : Unit {
body (...) {
let numberOfControls = Length(controls);
if (numberOfControls == 0) {
X(target);
}
elif (numberOfControls == 1) {
CNOT(Head(controls), target);
}
elif (numberOfControls == 2) {
CCNOT(controls[1], controls[0], target);
}
else {
let numberOfDirtyQubits = numberOfControls - 2;
borrowing (dirtyQubits = Qubit[numberOfDirtyQubits]) {
let allQubits = ([target] + dirtyQubits) + controls;
let lastDirtyQubit = numberOfDirtyQubits;
let totalNumberOfQubits = Length(allQubits);
let outerOperation1 = CCNOTByIndexLadder(numberOfDirtyQubits + 1, 1, 0, numberOfDirtyQubits, _);
let innerOperation = CCNOTByIndex(totalNumberOfQubits - 1, totalNumberOfQubits - 2, lastDirtyQubit, _);
WithA(outerOperation1, innerOperation, allQubits);
let outerOperation2 = CCNOTByIndexLadder(numberOfDirtyQubits + 2, 2, 1, numberOfDirtyQubits - 1, _);
WithA(outerOperation2, innerOperation, allQubits);
}
}
}
adjoint invert;
controlled (extraControls, ...) {
MultiControlledXBorrow(extraControls + controls, target);
}
controlled adjoint invert;
}
/// # Summary
/// Applies CCNOT to the qubits in target given by their indexes
operation CCNOTByIndex (control1Index : Int, control2Index : Int, targetIndex : Int, target : Qubit[]) : Unit {
body (...) {
CCNOT(target[control1Index], target[control2Index], target[targetIndex]);
}
adjoint invert;
}
/// # Summary
/// Repeatedly applies CCNOT to the qubits in target given by their indexes
/// Start with applying
operation CCNOTByIndexLadder (control1Index : Int, control2Index : Int, targetIndex : Int, count : Int, target : Qubit[]) : Unit {
body (...) {
for (i in 0 .. count - 1) {
CCNOTByIndex(i + control1Index, i + control2Index, targetIndex + i, target);
}
}
adjoint invert;
}
}
// /////////////////////////////////////////////////////////////////////////////////////////////
// Implementations of multiply controlled not gates not illustrated here
// /////////////////////////////////////////////////////////////////////////////////////////////
// ● The implementations that use all dirty or all clean ancilla are two extreme cases
// It is possible to interpolate between them and explore gate count / number of extra
// ancilla trade-offs
// /////////////////////////////////////////////////////////////////////////////////////////////