This repository was archived by the owner on Jan 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 181
Expand file tree
/
Copy pathArithmetic.qs
More file actions
111 lines (102 loc) · 3.88 KB
/
Arithmetic.qs
File metadata and controls
111 lines (102 loc) · 3.88 KB
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
namespace Microsoft.Quantum.Arithmetic {
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Canon;
open Microsoft.Quantum.Convert;
open Microsoft.Quantum.Arrays;
/// # Summary
/// Applies a bitwise-XOR operation between a classical integer and an
/// integer represented by a register of qubits.
///
/// # Description
/// Applies `X` operations to qubits in a little-endian register based on
/// 1 bits in an integer.
///
/// Let us denote `value` by a and let y be an unsigned integer encoded in `target`,
/// then `InPlaceXorLE` performs an operation given by the following map:
/// $\ket{y}\rightarrow \ket{y\oplus a}$ , where $\oplus$ is the bitwise exclusive OR operator.
///
/// # Input
/// ## value
/// An integer which is assumed to be non-negative.
/// ## target
/// A quantum register which is used to store `value` in little-endian encoding.
operation ApplyXorInPlace(value : Int, target : LittleEndian)
: Unit is Adj + Ctl {
ApplyToEachCA(
CControlledCA(X),
Zip(IntAsBoolArray(value, Length(target!)), target!)
);
}
/// # Summary
/// Applies a bitwise-XOR operation between a classical integer and a
/// big integer represented by a register of qubits.
///
/// # Description
/// Applies `X` operations to qubits in a little-endian register based on
/// 1 bits in a big integer.
///
/// Let us denote `value` by a and let y be an unsigned integer encoded in `target`,
/// then `InPlaceXorLE` performs an operation given by the following map:
/// $\ket{y}\rightarrow \ket{y\oplus a}$ , where $\oplus$ is the bitwise exclusive OR operator.
///
/// # Input
/// ## value
/// A big integer which is assumed to be non-negative.
/// ## target
/// A quantum register which is used to store `value` in little-endian encoding.
operation ApplyXorInPlaceL(value : BigInt, target : LittleEndian)
: Unit is Adj + Ctl {
ApplyToEachCA(
CControlledCA(X),
Zip(BigIntAsBoolArray(value), target!)
);
}
/// # Summary
/// Applies the three-qubit majority operation in-place on a register of
/// qubits.
///
/// # Description
/// This operation computes the majority function in-place on 3 qubits.
///
/// If we denote output qubit as $z$ and input qubits as $x$ and $y$,
/// the operation performs the following transformation:
/// $\ket{xyz} \rightarrow \ket{x \oplus z} \ket{y \oplus z} \ket{\operatorname{MAJ} (x, y, z)}$.
///
/// # Input
/// ## output
/// First input qubit. Note that the output is computed in-place
/// and stored in this qubit.
/// ## input
/// Second and third input qubits.
operation ApplyMajorityInPlace(output: Qubit, input: Qubit[])
: Unit is Adj + Ctl {
if (Length(input) == 2) {
MAJ(input[0], input[1], output);
} else {
fail $"The in-place majority operation on {Length(input)} is qubits not yet implemented.";
}
}
/// # Summary
/// Copies the most significant bit of a qubit register
/// `from` representing an unsigned integer into the qubit `target`.
///
/// # Input
/// ## from
/// The unsigned integer from which the highest bit is copied from.
/// the integer is encoded in little-endian format.
/// ## target
/// The qubit in which the highest bit is being copied. The bit encoding is
/// in computational basis.
///
/// # See Also
/// - LittleEndian
operation CopyMostSignificantBit (from : LittleEndian, target : Qubit) : Unit {
body (...) {
let mostSingificantQubit = Tail(from!);
CNOT(mostSingificantQubit, target);
}
adjoint invert;
}
}