Skip to content

Commit b341a1c

Browse files
author
Dmitry Vasilevsky
committed
Replaced custom ApplyAndAssuming0Target with AND from std
1 parent 2a4c335 commit b341a1c

File tree

5 files changed

+38
-107
lines changed

5 files changed

+38
-107
lines changed

library/std/src/Std/Arithmetic.qs

+2-2
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ operation RippleCarryCGIncByLE(xs : Qubit[], ys : Qubit[]) : Unit is Adj + Ctl {
225225
} else {
226226
use carries = Qubit[xsLen];
227227
within {
228-
ApplyAndAssuming0Target(xs[0], ys[0], carries[0]);
228+
AND(xs[0], ys[0], carries[0]);
229229
} apply {
230230
for i in 1..xsLen - 2 {
231231
CarryForInc(carries[i - 1], xs[i], ys[i], carries[i]);
@@ -307,7 +307,7 @@ operation LookAheadDKRSAddLE(xs : Qubit[], ys : Qubit[], zs : Qubit[]) : Unit is
307307
// with carry-out
308308
// compute initial generate values
309309
for k in 0..xsLen - 1 {
310-
ApplyAndAssuming0Target(xs[k], ys[k], zs[k + 1]);
310+
AND(xs[k], ys[k], zs[k + 1]);
311311
}
312312

313313
within {

library/std/src/Std/ArithmeticUtils.qs

+16-84
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,9 @@ operation HalfAdderForInc(x : Qubit, y : Qubit, carryOut : Qubit) : Unit is Adj
138138
use helper = Qubit();
139139

140140
within {
141-
ApplyAndAssuming0Target(x, y, helper);
141+
AND(x, y, helper);
142142
} apply {
143-
ApplyAndAssuming0Target(ctl, helper, carryOut);
143+
AND(ctl, helper, carryOut);
144144
}
145145
CCNOT(ctl, x, y);
146146
}
@@ -178,7 +178,7 @@ operation FullAdderForInc(carryIn : Qubit, x : Qubit, y : Qubit, carryOut : Qubi
178178
operation FullAdder(carryIn : Qubit, x : Qubit, y : Qubit, carryOut : Qubit) : Unit is Adj {
179179
CNOT(x, y);
180180
CNOT(x, carryIn);
181-
ApplyAndAssuming0Target(y, carryIn, carryOut);
181+
AND(y, carryIn, carryOut);
182182
CNOT(x, y);
183183
CNOT(x, carryOut);
184184
CNOT(y, carryIn);
@@ -190,7 +190,7 @@ operation CarryForInc(carryIn : Qubit, x : Qubit, y : Qubit, carryOut : Qubit) :
190190
body (...) {
191191
CNOT(carryIn, x);
192192
CNOT(carryIn, y);
193-
ApplyAndAssuming0Target(x, y, carryOut);
193+
AND(x, y, carryOut);
194194
CNOT(carryIn, carryOut);
195195
}
196196
adjoint auto;
@@ -209,7 +209,7 @@ operation CarryForInc(carryIn : Qubit, x : Qubit, y : Qubit, carryOut : Qubit) :
209209
operation UncarryForInc(carryIn : Qubit, x : Qubit, y : Qubit, carryOut : Qubit) : Unit is Adj + Ctl {
210210
body (...) {
211211
CNOT(carryIn, carryOut);
212-
Adjoint ApplyAndAssuming0Target(x, y, carryOut);
212+
Adjoint AND(x, y, carryOut);
213213
CNOT(carryIn, x);
214214
CNOT(x, y);
215215
}
@@ -220,92 +220,24 @@ operation UncarryForInc(carryIn : Qubit, x : Qubit, y : Qubit, carryOut : Qubit)
220220
let ctl = ctls[0];
221221

222222
CNOT(carryIn, carryOut);
223-
Adjoint ApplyAndAssuming0Target(x, y, carryOut);
223+
Adjoint AND(x, y, carryOut);
224224
CCNOT(ctl, x, y); // Controlled X(ctls + [x], y);
225225
CNOT(carryIn, x);
226226
CNOT(carryIn, y);
227227
}
228228
controlled adjoint auto;
229229
}
230230

231-
/// # Summary
232-
/// Applies AND gate between `control1` and `control2` and stores the result
233-
/// in `target` assuming `target` is in |0> state.
234-
///
235-
/// # Description
236-
/// Inverts `target` if and only if both controls are 1, but assumes that
237-
/// `target` is in state 0. The operation has T-count 4, T-depth 2 and
238-
/// requires no helper qubit, and may therefore be preferable to a CCNOT
239-
/// operation, if `target` is known to be 0.
240-
/// The adjoint of this operation is measurement based and requires no T
241-
/// gates (but requires target to support branching on measurements).
242-
/// Although the Toffoli gate (CCNOT) will perform faster in simulations,
243-
/// this version has lower T gate requirements.
244-
/// # References
245-
/// - Cody Jones: "Novel constructions for the fault-tolerant Toffoli gate",
246-
/// Phys. Rev. A 87, 022328, 2013
247-
/// [arXiv:1212.5069](https://arxiv.org/abs/1212.5069)
248-
/// doi:10.1103/PhysRevA.87.022328
249-
@Config(Adaptive)
250-
operation ApplyAndAssuming0Target(control1 : Qubit, control2 : Qubit, target : Qubit) : Unit is Adj {
251-
// NOTE: Eventually this operation will be public and intrinsic.
252-
body (...) {
253-
CCNOT(control1, control2, target);
254-
}
255-
adjoint (...) {
256-
H(target);
257-
if M(target) == One {
258-
Reset(target);
259-
CZ(control1, control2);
260-
}
261-
}
262-
}
263-
264231
operation ApplyOrAssuming0Target(control1 : Qubit, control2 : Qubit, target : Qubit) : Unit is Adj {
265232
within {
266233
X(control1);
267234
X(control2);
268235
} apply {
269-
ApplyAndAssuming0Target(control1, control2, target);
236+
AND(control1, control2, target);
270237
X(target);
271238
}
272239
}
273240

274-
/// # Summary
275-
/// Applies AND gate between `control1` and `control2` and stores the result
276-
/// in `target` assuming `target` is in |0> state.
277-
///
278-
/// # Description
279-
/// Inverts `target` if and only if both controls are 1, but assumes that
280-
/// `target` is in state 0. The operation has T-count 4, T-depth 2 and
281-
/// requires no helper qubit, and may therefore be preferable to a CCNOT
282-
/// operation, if `target` is known to be 0.
283-
/// This version is suitable for Base profile.
284-
/// Although the Toffoli gate (CCNOT) will perform faster in simulations,
285-
/// this version has lower T gate requirements.
286-
/// # References
287-
/// - Cody Jones: "Novel constructions for the fault-tolerant Toffoli gate",
288-
/// Phys. Rev. A 87, 022328, 2013
289-
/// [arXiv:1212.5069](https://arxiv.org/abs/1212.5069)
290-
/// doi:10.1103/PhysRevA.87.022328
291-
@Config(not Adaptive)
292-
operation ApplyAndAssuming0Target(control1 : Qubit, control2 : Qubit, target : Qubit) : Unit is Adj {
293-
H(target);
294-
T(target);
295-
CNOT(control1, target);
296-
CNOT(control2, target);
297-
within {
298-
CNOT(target, control1);
299-
CNOT(target, control2);
300-
} apply {
301-
Adjoint T(control1);
302-
Adjoint T(control2);
303-
T(target);
304-
}
305-
H(target);
306-
S(target);
307-
}
308-
309241
/// # Summary
310242
/// Computes carries for the look-ahead adder
311243
operation ComputeCarries(ps : Qubit[], gs : Qubit[]) : Unit is Adj {
@@ -318,8 +250,8 @@ operation ComputeCarries(ps : Qubit[], gs : Qubit[]) : Unit is Adj {
318250
let registerPartition = MappedOverRange(t -> Floor(IntAsDouble(n) / IntAsDouble(2^t)) - 1, 1..T - 1);
319251
let pWorkspace = [ps] + Partitioned(registerPartition, qs);
320252

321-
// Note that we cannot use ApplyAndAssuming0Target targeting gs[0]
322-
// as it may not be in the 0 state. We use regular CCNOT in GRounds and CRounds.
253+
// Note that we cannot use AND gate targeting gs[0] as it may not be in the 0 state.
254+
// We use regular CCNOT in GRounds and CRounds.
323255
within {
324256
PRounds(pWorkspace);
325257
} apply {
@@ -354,7 +286,7 @@ operation PRounds(pWorkspace : Qubit[][]) : Unit is Adj {
354286
let (current, next) = (Rest(ws[0]), ws[1]);
355287

356288
for m in IndexRange(next) {
357-
ApplyAndAssuming0Target(current[2 * m], current[2 * m + 1], next[m]);
289+
AND(current[2 * m], current[2 * m + 1], next[m]);
358290
}
359291
}
360292
}
@@ -447,7 +379,7 @@ operation ApplyActionIfGreaterThanOrEqualConstant<'T>(
447379

448380
within {
449381
for i in 0..Length(cs1) - 1 {
450-
let op = cNormalized &&& (1L <<< (i + 1)) != 0L ? ApplyAndAssuming0Target | ApplyOrAssuming0Target;
382+
let op = cNormalized &&& (1L <<< (i + 1)) != 0L ? AND | ApplyOrAssuming0Target;
451383
op(cs1[i], xNormalized[i + 1], qs[i]);
452384
}
453385
} apply {
@@ -510,7 +442,7 @@ operation CarryWith1CarryIn(
510442
body (...) {
511443
X(x);
512444
X(y);
513-
ApplyAndAssuming0Target(x, y, carryOut);
445+
AND(x, y, carryOut);
514446
X(carryOut);
515447
}
516448

@@ -568,10 +500,10 @@ operation LogDepthAndChain(ctls : Qubit[], tgts : Qubit[]) : Unit is Adj {
568500
Fact(lc == lt + 1, $"There must be exactly one more control qubit than target qubits (got {lc}, {lt})");
569501

570502
if lt == 1 {
571-
ApplyAndAssuming0Target(ctls[0], ctls[1], tgts[0]);
503+
AND(ctls[0], ctls[1], tgts[0]);
572504
} elif lt == 2 {
573-
ApplyAndAssuming0Target(ctls[0], ctls[1], tgts[0]);
574-
ApplyAndAssuming0Target(ctls[2], tgts[0], tgts[1]);
505+
AND(ctls[0], ctls[1], tgts[0]);
506+
AND(ctls[2], tgts[0], tgts[1]);
575507
} else {
576508
let left = lc / 2;
577509
let right = lc - left;
@@ -584,6 +516,6 @@ operation LogDepthAndChain(ctls : Qubit[], tgts : Qubit[]) : Unit is Adj {
584516

585517
LogDepthAndChain(ctlsLeft, tgtsLeft);
586518
LogDepthAndChain(ctlsRight, tgtsRight);
587-
ApplyAndAssuming0Target(Tail(tgtsLeft), Tail(tgtsRight), Tail(tgts));
519+
AND(Tail(tgtsLeft), Tail(tgtsRight), Tail(tgts));
588520
}
589521
}

library/std/src/Std/TableLookup.qs

+5-6
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ import
1212
Std.Convert.IntAsDouble,
1313
Std.Arrays.*,
1414
Std.ResourceEstimation.BeginEstimateCaching,
15-
Std.ResourceEstimation.EndEstimateCaching,
16-
Std.ArithmeticUtils.ApplyAndAssuming0Target;
15+
Std.ResourceEstimation.EndEstimateCaching;
1716

1817
/// # Summary
1918
/// Performs table lookup using a SELECT network
@@ -123,7 +122,7 @@ operation SinglyControlledSelect(
123122
within {
124123
X(tail);
125124
} apply {
126-
ApplyAndAssuming0Target(ctl, tail, helper);
125+
AND(ctl, tail, helper);
127126
}
128127

129128
SinglyControlledSelect(helper, parts[0], most, target);
@@ -132,7 +131,7 @@ operation SinglyControlledSelect(
132131

133132
SinglyControlledSelect(helper, parts[1], most, target);
134133

135-
Adjoint ApplyAndAssuming0Target(ctl, tail, helper);
134+
Adjoint AND(ctl, tail, helper);
136135
}
137136

138137
EndEstimateCaching();
@@ -240,7 +239,7 @@ operation EncodeUnary(
240239
// targets are the first and second 2^i qubits of the target register
241240
let split = Partitioned([2^i, 2^i], target);
242241
for j in IndexRange(split[0]) {
243-
ApplyAndAssuming0Target(input[i], split[0][j], split[1][j]);
242+
AND(input[i], split[0][j], split[1][j]);
244243
CNOT(split[1][j], split[0][j]);
245244
}
246245
}
@@ -275,7 +274,7 @@ operation AndChainOperation(ctls : Qubit[], helper : Qubit[], target : Qubit) :
275274
let tgts = helper + [target];
276275

277276
for idx in IndexRange(tgts) {
278-
ApplyAndAssuming0Target(ctls1[idx], ctls2[idx], tgts[idx]);
277+
AND(ctls1[idx], ctls2[idx], tgts[idx]);
279278
}
280279
}
281280
}

samples/algorithms/Shor.qs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import Std.Arithmetic.*;
1414
import Std.Arrays.*;
1515

1616
operation Main() : (Int, Int) {
17-
let n = 143; // 11*13;
17+
let n = 187; // 11*17;
1818
// You can try these other examples for a lengthier computation.
1919
// let n = 16837; // = 113*149
2020
// let n = 22499; // = 149*151

samples_test/src/tests/algorithms.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -237,23 +237,23 @@ pub const QUANTUMHELLOWORLD_EXPECT_DEBUG: Expect = expect![[r#"
237237
pub const RANDOMBIT_EXPECT: Expect = expect!["Zero"];
238238
pub const RANDOMBIT_EXPECT_DEBUG: Expect = expect!["Zero"];
239239
pub const SHOR_EXPECT: Expect = expect![[r#"
240-
*** Factorizing 143, attempt 1.
241-
Estimating period of 139.
240+
*** Factorizing 187, attempt 1.
241+
Estimating period of 182.
242242
Estimating frequency with bitsPrecision=17.
243-
Estimated frequency=30583
244-
Found period=30
245-
Found factor=13
246-
Found factorization 143 = 13 * 11
247-
(13, 11)"#]];
243+
Estimated frequency=126158
244+
Found period=80
245+
Found factor=17
246+
Found factorization 187 = 17 * 11
247+
(17, 11)"#]];
248248
pub const SHOR_EXPECT_DEBUG: Expect = expect![[r#"
249-
*** Factorizing 143, attempt 1.
250-
Estimating period of 139.
249+
*** Factorizing 187, attempt 1.
250+
Estimating period of 182.
251251
Estimating frequency with bitsPrecision=17.
252-
Estimated frequency=30583
253-
Found period=30
254-
Found factor=13
255-
Found factorization 143 = 13 * 11
256-
(13, 11)"#]];
252+
Estimated frequency=126158
253+
Found period=80
254+
Found factor=17
255+
Found factorization 187 = 17 * 11
256+
(17, 11)"#]];
257257
pub const SIMPLEISING_EXPECT: Expect =
258258
expect!["[Zero, Zero, Zero, One, One, Zero, One, One, Zero]"];
259259
pub const SIMPLEISING_EXPECT_DEBUG: Expect =

0 commit comments

Comments
 (0)