Skip to content

Commit d9e4aa6

Browse files
DmitryVasilevskyDmitry Vasilevsky
and
Dmitry Vasilevsky
authored
OpenQASM Bernstein-Vazirani sample (#2403)
Co-authored-by: Dmitry Vasilevsky <dmitryv@microsoft.com>
1 parent 88f1a04 commit d9e4aa6

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// OpenQASM Bernstein-Vazirani sample
2+
//
3+
// This sample demonstrates the Bernstein-Vazirani algorithm,
4+
// which determines the value of a bit string encoded in a function.
5+
6+
OPENQASM 3;
7+
include "stdgates.inc";
8+
9+
// Define the number of qubits.
10+
const int nQubits = 5;
11+
// The secret bit string to be determined.
12+
const bit[nQubits] secretBitString = "10101";
13+
14+
// Given bit string 𝑟⃗ = (r₀, …, rₙ₋₁), represented as an array of bits,
15+
// this operation applies a unitary 𝑈 that acts on 𝑛 + 1 qubits as:
16+
// 𝑈 |𝑥〉|𝑦〉 = |𝑥〉|𝑦 ⊕ 𝑓(𝑥)〉
17+
// where 𝑓(𝑥) = Σᵢ 𝑥ᵢ 𝑟ᵢ mod 2.
18+
def ApplyParityOperation(
19+
bit[nQubits] bitStringAsBoolArray,
20+
qubit[nQubits] xRegister,
21+
qubit yQubit ) {
22+
23+
// Apply the quantum operations that encode the secret bit string.
24+
for int i in [0:nQubits-1] {
25+
if (bitStringAsBoolArray[i]) {
26+
cx xRegister[i], yQubit;
27+
}
28+
}
29+
}
30+
31+
// Applies parity operation for a particular secret bit string.
32+
def ParityOperationForSecretBitstring(qubit[nQubits] xRegister, qubit yQubit) {
33+
ApplyParityOperation(secretBitString, xRegister, yQubit);
34+
}
35+
36+
// Given a register in the all-zeros state, prepares a uniform
37+
// superposition over all basis states.
38+
def PrepareUniform(qubit[nQubits] q) {
39+
for int i in [0:nQubits-1] {
40+
h q[i];
41+
}
42+
}
43+
44+
// This operation implements the Bernstein-Vazirani quantum algorithm.
45+
// This algorithm computes for a given Boolean function that is promised to
46+
// be a parity 𝑓(𝑥₀, …, 𝑥ₙ₋₁) = Σᵢ 𝑟ᵢ 𝑥ᵢ a result in the form of a bit
47+
// vector (𝑟₀, …, 𝑟ₙ₋₁) corresponding to the parity function.
48+
// Note that it is promised that the function is actually a parity
49+
// function.
50+
def BernsteinVazirani(qubit[nQubits] queryRegister, qubit target) -> bit[nQubits] {
51+
bit[nQubits] results;
52+
53+
// The target qubit needs to be flipped so that a relative phase is
54+
// introduced when we apply a Hadamard gate and we can use
55+
// phase kickback when parity operation is applied.
56+
x target;
57+
h target;
58+
59+
// Prepare the query register in a uniform superposition.
60+
PrepareUniform(queryRegister);
61+
62+
// Apply the parity operation.
63+
ParityOperationForSecretBitstring(queryRegister, target);
64+
65+
// Uncompute the preparation of the uniform superposition.
66+
PrepareUniform(queryRegister);
67+
68+
// Measure the qubits
69+
results = measure queryRegister;
70+
71+
// The string we are looking for is returned after execution.
72+
return results;
73+
}
74+
75+
// Main program
76+
77+
// Initialize the qubits
78+
qubit[nQubits] queryRegister;
79+
qubit target;
80+
81+
reset queryRegister;
82+
reset target;
83+
84+
// This register will hold and return the bit string found by the algorithm.
85+
output bit[nQubits] results;
86+
87+
// Call the Bernstein-Vazirani algorithm to find the secret bit string.
88+
results = BernsteinVazirani(queryRegister, target);

0 commit comments

Comments
 (0)