microsoft/qdk

Public

mirrored fromhttps://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
9831093db0098b3a3e55cbadf3929222d7dd4805

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

samples/OpenQASM/BernsteinVazirani.qasm

88lines ยท modecode

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
6OPENQASM 3;
7include "stdgates.inc";
8
9// Define the number of qubits.
10const int nQubits = 5;
11// The secret bit string to be determined.
12const 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.
18def 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.
32def 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.
38def 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.
50def 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
78qubit[nQubits] queryRegister;
79qubit target;
80
81reset queryRegister;
82reset target;
83
84// This register will hold and return the bit string found by the algorithm.
85output bit[nQubits] results;
86
87// Call the Bernstein-Vazirani algorithm to find the secret bit string.
88results = BernsteinVazirani(queryRegister, target);
89