Skip to content

Commit 1dab5f3

Browse files
committed
Add more examples
1 parent 69174bb commit 1dab5f3

7 files changed

Lines changed: 37 additions & 9 deletions

File tree

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,8 @@ Examples of common gates (and of the syntax) can be found in [examples/gates.ph]
3737
```bash
3838
cargo run -- --file examples/gates.ph
3939
```
40+
The examples folder also contains programs for:
41+
- Grover's algorithm (for finding the element 0000)
42+
- QFT (up to 4 qubits)
43+
- GHZ state preparation
44+
- An example circuit translation with swap gates

examples/ghz.ph

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
gate Z = if let |1> then -1,
2+
gate X = if let |-> then -1,
3+
gate S = sqrt(Z),
4+
gate Y = if let S . |-> then -1,
5+
gate H = if let sqrt(sqrt(Y)) . |1> then -1,
6+
7+
H x id4; if let |1> x id4 then X x X x X x X

examples/grover.ph

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
gate oracle = if let |0000> then -1,
2+
gate diffusion = if let |++++> then -1,
3+
4+
oracle ; diffusion

examples/qft.ph

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
gate Z = if let |1> then -1,
2+
gate X = if let |-> then -1,
3+
gate S = sqrt(Z),
4+
gate Y = if let S . |-> then -1,
5+
gate H = if let sqrt(sqrt(Y)) . |1> then -1,
6+
gate R2 = if let |1> then i,
7+
gate R3 = if let |1> then ph(0.25pi),
8+
gate R4 = if let |1> then ph(0.125pi),
9+
10+
gate QFT0 = id0,
11+
gate QFT1 = H x id0; if let |1> x id0 then id0; id x QFT0,
12+
gate QFT2 = H x id; if let |1> x id1 then R2; id x QFT1,
13+
gate QFT3 = H x id2; if let |1> x id2 then R2 x R3; id x QFT2,
14+
gate QFT4 = H x id3; if let |1> x id3 then R2 x R3 x R4; id x QFT3,
15+
16+
QFT4

src/raw_syntax/term.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ pub enum AtomRInner<S> {
7979
/// Pattern to match on in "if let"
8080
pattern: PatternR<S>,
8181
/// Body of the "if let"
82-
inner: Box<AtomR<S>>,
82+
inner: Box<TensorR<S>>,
8383
},
8484
/// Top level symbol, a named gate
8585
Gate(Name),
@@ -267,7 +267,7 @@ impl HasParser for AtomRInner<Range<usize>> {
267267
_: multispace1,
268268
_: "then".context(StrContext::Expected(StrContextValue::StringLiteral("then"))),
269269
_: multispace1,
270-
AtomR::parser)),
270+
TensorR::parser)),
271271
)
272272
.map(|(pattern, inner)| AtomRInner::IfLet {
273273
pattern,

src/typecheck.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@
33
use std::collections::HashMap;
44

55
use crate::{
6-
raw_syntax::{
7-
PatternR, TermR,
8-
pattern::PatTensorR,
9-
term::{AtomR, TensorR},
10-
},
6+
raw_syntax::{PatternR, TermR, pattern::PatTensorR, term::TensorR},
117
text::Name,
128
typed_syntax::{PatternType, TermT, TermType},
139
};
@@ -33,7 +29,7 @@ pub enum TypeCheckError<S> {
3329
/// Type of pattern
3430
pty: PatternType,
3531
/// Body term
36-
t: AtomR<S>,
32+
t: TensorR<S>,
3733
/// Type of body term
3834
tty: TermType,
3935
},

src/typed_syntax/term.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ impl TermT {
217217
TermT::Phase(phase) => AtomRInner::Phase(*phase),
218218
TermT::IfLet { pattern, inner } => AtomRInner::IfLet {
219219
pattern: pattern.to_raw(),
220-
inner: Box::new(inner.to_raw_atom()),
220+
inner: Box::new(inner.to_raw_tensor()),
221221
},
222222
TermT::Gate { name, .. } => AtomRInner::Gate(name.to_owned()),
223223
TermT::Inverse(inner) => AtomRInner::Inverse(Box::new(inner.to_raw_atom())),

0 commit comments

Comments
 (0)