Skip to content

Commit ea2290d

Browse files
WIP: Add QFT example
1 parent a77acd7 commit ea2290d

File tree

3 files changed

+50
-6
lines changed

3 files changed

+50
-6
lines changed

examples/qft.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use std::f64::consts::PI;
2+
3+
use anyhow::Result;
4+
use simple_qsim::{circuit::GateKind, Circuit, QState};
5+
6+
fn main() -> Result<()> {
7+
let num_of_qubits = 3;
8+
9+
let mut circuit = Circuit::new(num_of_qubits);
10+
11+
// Initialize input with H
12+
for i in 0..num_of_qubits {
13+
circuit.add_gate(GateKind::H, i);
14+
}
15+
16+
for i in (0..num_of_qubits).rev() {
17+
circuit.add_gate(GateKind::H, i);
18+
for j in (0..i).rev() {
19+
let angle = 2.0 * PI / 2.0f64.powi((num_of_qubits - j) as i32);
20+
circuit.add_control(i, j, GateKind::Phase(angle))?;
21+
}
22+
}
23+
24+
println!("Circuit:\n{}", circuit);
25+
26+
let qs = QState::from_str("000")?;
27+
28+
let result = circuit.apply(&qs)?;
29+
println!("Resulting state:\n{}", result);
30+
31+
Ok(())
32+
}

src/circuit.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use nalgebra_sparse::{coo::CooMatrix, csr::CsrMatrix};
44
use num_complex::Complex;
55

66
use crate::gates::{
7-
h_matrix, inv_t_matrix, rx_dence_matrix, rx_matrix, ry_dence_matrix, ry_matrix,
7+
h_matrix, inv_t_matrix, phase_matrix, rx_dence_matrix, rx_matrix, ry_dence_matrix, ry_matrix,
88
rz_dence_matrix, rz_matrix, s_dence_matrix, s_matrix, t_matrix, x_dence_matrix, x_matrix,
99
y_dence_matrix, y_matrix, z_dence_matrix, z_matrix,
1010
};
@@ -40,6 +40,7 @@ pub enum GateKind {
4040
RX(f64),
4141
RY(f64),
4242
RZ(f64),
43+
Phase(f64),
4344
}
4445

4546
pub enum ParameterizedGate {
@@ -190,14 +191,19 @@ impl Circuit {
190191
self.gate_at(index, GateKind::H)
191192
}
192193

193-
pub fn control(mut self, control: usize, target: usize, kind: GateKind) -> Result<Self> {
194+
pub fn add_control(&mut self, control: usize, target: usize, kind: GateKind) -> Result<()> {
194195
self.gates.push(Gate {
195196
kind,
196197
index: GateIndex::Control {
197198
controls: vec![control],
198199
target,
199200
},
200201
});
202+
Ok(())
203+
}
204+
205+
pub fn control(mut self, control: usize, target: usize, kind: GateKind) -> Result<Self> {
206+
self.add_control(control, target, kind)?;
201207
Ok(self)
202208
}
203209

@@ -372,6 +378,7 @@ impl Circuit {
372378
GateKind::RX(angle) => rx_matrix(*angle),
373379
GateKind::RY(angle) => ry_matrix(*angle),
374380
GateKind::RZ(angle) => rz_matrix(*angle),
381+
GateKind::Phase(angle) => phase_matrix(*angle),
375382
};
376383
Ok(matrix)
377384
}

src/gates.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,8 @@ pub fn ry_dence_matrix(angle: f64) -> Matrix2<Qbit> {
5656

5757
pub fn rz_matrix(angle: f64) -> CsrMatrix<Qbit> {
5858
let mut rz_coo = CooMatrix::new(2, 2);
59-
rz_coo.push(0, 0, Complex::from_polar(1.0, -angle / 2.0).exp());
60-
// rz_coo.push(0, 1, Complex::ZERO);
61-
// rz_coo.push(1, 0, Complex::ZERO);
62-
rz_coo.push(1, 1, Complex::from_polar(1.0, angle / 2.0).exp());
59+
rz_coo.push(0, 0, Complex::from_polar(1.0, -angle / 2.0));
60+
rz_coo.push(1, 1, Complex::from_polar(1.0, angle / 2.0));
6361
CsrMatrix::from(&rz_coo)
6462
}
6563

@@ -72,6 +70,13 @@ pub fn rz_dence_matrix(angle: f64) -> Matrix2<Qbit> {
7270
])
7371
}
7472

73+
pub fn phase_matrix(angle: f64) -> CsrMatrix<Qbit> {
74+
let mut phase_coo = CooMatrix::new(2, 2);
75+
phase_coo.push(0, 0, Complex::ONE);
76+
phase_coo.push(1, 1, Complex::from_polar(1.0, angle));
77+
CsrMatrix::from(&phase_coo)
78+
}
79+
7580
pub fn x_matrix() -> CsrMatrix<Qbit> {
7681
let mut x_coo = CooMatrix::new(2, 2);
7782
x_coo.push(0, 1, Complex::new(1.0, 0.0));

0 commit comments

Comments
 (0)