Skip to content

Commit 285baa4

Browse files
DmitryVasilevskyDmitry Vasilevsky
and
Dmitry Vasilevsky
authored
Added three OpenQASM samples (#2394)
Added three OpenQASM samples. * OpenQasmHelloWorld.qasm * BellPair.qasm * RandomNumber.qasm For now, the samples are only added as files. No tests are added, and they are not available as quick templates. --------- Co-authored-by: Dmitry Vasilevsky <dmitryv@microsoft.com>
1 parent e570222 commit 285baa4

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

samples/OpenQASM/BellPair.qasm

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// OpenQASM Bell Pair sample
2+
//
3+
// Bell pairs are specific quantum states of two qubits that represent
4+
// the simplest (and maximal) examples of quantum entanglement. This sample
5+
// prepares |Φ⁺⟩ = (|00⟩+|11⟩)/√2.
6+
//
7+
// See [Bell state](https://en.wikipedia.org/wiki/Bell_state)
8+
9+
OPENQASM 3;
10+
include "stdgates.inc";
11+
12+
// Declares use of qubits `q1` and `q2`.
13+
qubit q1;
14+
qubit q2;
15+
16+
// Qubits are initially in an undefined state.
17+
// Reset is used here to initialize qubits to |0⟩ state.
18+
reset q1;
19+
reset q2;
20+
21+
// Set qubit `q1` in superposition of |0⟩ and |1⟩ by applying a Hadamard gate.
22+
h q1;
23+
// Entangle the two qubits `q1` and `q2` using the `cx` gate.
24+
cx q1, q2;
25+
26+
// Measure the two qubits and store results in classical variables `r1` and `r2`.
27+
bit r1 = measure q1;
28+
bit r2 = measure q2;
29+
30+
// Note, that the reported result (r1, r2)
31+
// will always be either (Zero, Zero) or (One, One).
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// OpenQASM Hello World sample
2+
//
3+
// This is one of the simplest OpenQASM programs that contains quantum part.
4+
// It uses one qubit, resets it and and immediately measures it.
5+
// Since the qubit is in |0〉 state after reset
6+
// such measurement will always yield `Zero`.
7+
8+
// OpenQASM version identifier.
9+
OPENQASM 3;
10+
11+
// Declares use of a single qubit and names it `q`.
12+
// All qubits must be declared in a global scope.
13+
qubit q;
14+
15+
// Qubits are initially in an undefined state.
16+
// Reset is used here to initialize qubit to a |0〉 state.
17+
reset q;
18+
19+
// Measures qubit and stores the result in a classical bit.
20+
// The qubit is in |0〉 state after reset, so the `b` will be 0.
21+
bit b = measure q;
22+
23+
// Note, that the content of all classical variables
24+
// are reported to the user at the end of the program
25+
// unless explicit output declarations exist.

samples/OpenQASM/RandomNumber.qasm

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// OpenQASM Quantum Random Number Generator
2+
//
3+
// This program implements a quantum random number generator by setting qubits
4+
// in superposition and then using the measurement results as random bits.
5+
6+
OPENQASM 3;
7+
include "stdgates.inc";
8+
9+
// Generates one random bit using a qubit `q`.
10+
def GenerateRandomBit(qubit q) -> bit {
11+
// Resets qubit `q` to |0〉 state
12+
reset q;
13+
// Sets the qubit into superposition of 0 and 1 using the Hadamard gate.
14+
h q;
15+
16+
// At this point qubit `q` has 50% chance of being measured in the |0〉 state
17+
// and 50% chance of being measured in the |1〉 state.
18+
bit b = measure q;
19+
20+
// Return the measurement result - a random bit.
21+
return b;
22+
}
23+
24+
// Generates a random integer with `nBit` bits using qubit `q`
25+
def GenerateRandomNumber(qubit q, int nBits) -> int {
26+
int number = 0;
27+
28+
// Loop `nBits` times to generate `nBits` random bits.
29+
for int k in [1:nBits] {
30+
// Shift the number left by 1 to make space for the next bit.
31+
number <<= 1;
32+
// Set the least significant bit of the number to a random bit.
33+
number |= GenerateRandomBit(q);
34+
}
35+
36+
// Return the random number.
37+
return number;
38+
}
39+
40+
// User one qubit `q`.
41+
qubit q;
42+
43+
// Generate a 5-bit random number using the qubit `q`.
44+
int random = GenerateRandomNumber(q, 5);

0 commit comments

Comments
 (0)