Skip to content

Commit 7313a6e

Browse files
Bring back RGN samples (#2306)
Brings back the QRNGNISQ sample under Algorithms.
1 parent 1287d30 commit 7313a6e

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

samples/algorithms/QRNGNISQ.qs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/// # Sample
2+
/// Quantum Random Number Generator
3+
///
4+
/// # Description
5+
/// This program implements a quantum random number generator by setting qubits
6+
/// in superposition and then using the measurement results as random bits.
7+
import Std.Measurement.*;
8+
import Std.Intrinsic.*;
9+
10+
operation Main() : Result[] {
11+
// Generate 5-bit random number.
12+
let nBits = 5;
13+
return GenerateNRandomBits(nBits);
14+
}
15+
16+
/// # Summary
17+
/// Generates N random bits.
18+
operation GenerateNRandomBits(nBits : Int) : Result[] {
19+
// Allocate N qubits.
20+
use register = Qubit[nBits];
21+
22+
// Set the qubits into superposition of 0 and 1 using the Hadamard
23+
// operation `H`.
24+
for qubit in register {
25+
H(qubit);
26+
}
27+
28+
// At this point each has 50% chance of being measured in the |0〉 state
29+
// and 50% chance of being measured in the |1〉 state.
30+
// Measure each qubit and reset them all so they can be safely
31+
// deallocated.
32+
let results = MResetEachZ(register);
33+
return results;
34+
}

samples_test/src/tests/algorithms.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ pub const QRNG_EXPECT: Expect = expect![[r#"
134134
pub const QRNG_EXPECT_DEBUG: Expect = expect![[r#"
135135
Sampling a random number between 0 and 100:
136136
46"#]];
137+
pub const QRNGNISQ_EXPECT: Expect = expect!["[Zero, Zero, One, One, One]"];
138+
pub const QRNGNISQ_EXPECT_DEBUG: Expect = expect!["[Zero, Zero, One, One, One]"];
137139
pub const SHOR_EXPECT: Expect = expect![[r#"
138140
*** Factorizing 187, attempt 1.
139141
Estimating period of 182.

0 commit comments

Comments
 (0)