Skip to content

Commit

Permalink
Merge pull request #14 from notmgsk/3-less-panics
Browse files Browse the repository at this point in the history
Catch out-of-bounds errors when creating gate matrices
  • Loading branch information
notmgsk authored Jul 17, 2021
2 parents 49116d9 + 4ee5cd1 commit af4c703
Show file tree
Hide file tree
Showing 6 changed files with 277 additions and 167 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ thiserror = "1.0.24"
anyhow = "1.0.39"

[dev-dependencies]
pretty_assertions = "0.7.2"
pretty_assertions = "0.7.2"
39 changes: 9 additions & 30 deletions src/gates/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,20 @@ pub mod standard;
use ndarray::{Array, Array2};
use num::complex::Complex64;

use crate::gates::standard::{ccnot, cnot, cz, h, i, rx, ry, rz, swap, x, z};
use crate::gates::standard::swap;

#[derive(Default, Clone, Debug)]
pub struct QGate {
matrix: Array2<Complex64>,
qubits: Vec<usize>,
}

pub fn gate_matrix(name: String, params: Vec<f64>, qubits: Vec<usize>) -> QGate {
match name.as_str() {
"I" => i(qubits[0]),
"X" => x(qubits[0]),
"Y" => x(qubits[0]),
"Z" => z(qubits[0]),
"RX" => rx(params[0], qubits[0]),
"RY" => ry(params[0], qubits[0]),
"RZ" => rz(params[0], qubits[0]),
"H" => h(qubits[0]),
"CZ" => cz(qubits[0], qubits[1]),
"CNOT" => cnot(qubits[0], qubits[1]),
"SWAP" => swap(qubits[0], qubits[1]),
"CCNOT" => ccnot(qubits[0], qubits[1], qubits[2]),
_ => todo!(),
}
}

// TODO Is it possible to define a SquareArray2 type (where dimensions are equal)
// TODO Likewise, an array type which is both square and each dimension is 2^n

impl QGate {
fn lift_adjacent(&self, i: usize, n_qubits: usize) -> Array2<Complex64> {
let gate_size = self.qubits.len();
let n = num::pow(2, i);
let bottom = Array::eye(n);
let top =
Array::eye(2u64.pow((n_qubits as u32) - (i as u32) - (gate_size as u32)) as usize);
let top = Array::eye(2u64.pow((n_qubits - i - gate_size) as u32) as usize);

kron(&top, &kron(&self.matrix, &bottom))
}
Expand Down Expand Up @@ -116,11 +94,12 @@ mod tests {
use ndarray::arr2;
use pretty_assertions::assert_eq;

use crate::gates::standard::{cnot, x};
use crate::matrix::{C0, C1};

#[test]
fn lift_1q() {
let gate = super::x(0);
let gate = x(0);
assert_eq!(
gate.lift_adjacent(0, 2),
arr2(&[
Expand All @@ -131,7 +110,7 @@ mod tests {
])
);

let gate = super::x(1);
let gate = x(1);
assert_eq!(
gate.lift_adjacent(1, 2),
arr2(&[
Expand All @@ -150,7 +129,7 @@ mod tests {

#[test]
fn lift_2q() {
let gate = super::cnot(0, 1);
let gate = cnot(0, 1);
assert_eq!(
gate.lift(2),
arr2(&[
Expand All @@ -161,7 +140,7 @@ mod tests {
])
);

let gate = super::cnot(1, 0);
let gate = cnot(1, 0);
assert_eq!(
gate.lift(2),
arr2(&[
Expand All @@ -172,7 +151,7 @@ mod tests {
])
);

let gate = super::cnot(0, 2);
let gate = cnot(0, 2);
assert_eq!(
gate.lift(3),
arr2(&[
Expand All @@ -187,7 +166,7 @@ mod tests {
])
);

let gate = super::cnot(2, 0);
let gate = cnot(2, 0);
assert_eq!(
gate.lift(3),
arr2(&[
Expand Down
Loading

0 comments on commit af4c703

Please sign in to comment.