Skip to content

Commit 187d93d

Browse files
committed
Split toss example in multiple modules
Showcase `import` capability
1 parent 477a70e commit 187d93d

File tree

3 files changed

+38
-33
lines changed

3 files changed

+38
-33
lines changed

examples/math.ql

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
fn sin(x: f64) : f64 {
2+
return 0.0;
3+
}
4+
5+
fn cos(x: f64) : f64 {
6+
return 1.0;
7+
}
8+
9+
fn exp(x: f64) : f64 {
10+
let e = 2.718;
11+
e * x
12+
}
13+

examples/std.ql

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import math::sin;
2+
import math::cos;
3+
import math::exp;
4+
5+
fn U (theta: f64, phi: f64, lambda: f64, q0: !qbit) : qbit {
6+
// U (θ,φ,λ) = [ε cos(θ/2) -ε'sin(θ/2)]
7+
// [ε'sin(θ/2) ε cos(θ/2)]
8+
// where ε = e^(-i(φ+λ)/2) and ε' = e^(-i(φ-λ)/2)
9+
let e0 = exp ((phi + lambda) / 2);
10+
let e1 = exp ((phi - lambda) / 2);
11+
let a = theta / 2;
12+
13+
// This equation is the bread and butter for our transformations.
14+
let transform = [[e0 * cos(a), -e1 * sin(a)], [e1 * sin(a), e0 * cos(a)]];
15+
16+
// return the transformed qubit
17+
return transform * q0;
18+
}
19+
20+
fn Hadamard(q: qbit) : qbit {
21+
let pi: f64 = 3.14;
22+
return U(pi/2, 0, 0, q);
23+
}
24+

examples/toss.ql

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,4 @@
1-
// For small values
2-
fn sin(x: f64) : f64 {
3-
x
4-
}
5-
6-
// For small values
7-
fn cos(x: f64) : f64 {
8-
1 - x
9-
}
10-
11-
fn exp(x: f64) : f64 {
12-
2.718 ** x
13-
}
14-
15-
fn U (theta: f64, phi: f64, lambda: f64, q0: qbit) : qbit {
16-
// U (θ,φ,λ) = [ε cos(θ/2) -ε'sin(θ/2)]
17-
// [ε'sin(θ/2) ε cos(θ/2)]
18-
// where ε = e^(-i(φ+λ)/2) and ε' = e^(-i(φ-λ)/2)
19-
let e0 = exp ((phi + lambda) / 2);
20-
let e1 = exp ((phi - lambda) / 2);
21-
let a = theta / 2;
22-
23-
// This equation is the bread and butter for our transformations.
24-
let transform = [[e0 * cos(a), -e1 * sin(a)], [e1 * sin(a), e0 * cos(a)]];
25-
26-
// return the transformed qubit
27-
return transform * q0;
28-
}
29-
30-
fn Hadamard(q: qbit) : qbit {
31-
let pi = 3.14;
32-
return U(pi/2, 0, 0, q);
33-
}
1+
import std::Hadamard;
342

353
fn toss() : qbit {
364
let zero_state = 0q(0, 1); // represent a qubit in zero state simply as 0

0 commit comments

Comments
 (0)