Skip to content

Commit

Permalink
Breaking: instruction_matrix returns a Result
Browse files Browse the repository at this point in the history
  • Loading branch information
notmgsk committed Jul 15, 2021
1 parent b25d9db commit 43a01cf
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 19 deletions.
3 changes: 2 additions & 1 deletion 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 @@ -8,7 +8,7 @@ edition = "2018"

[dependencies]
structopt = "0.3.13"
quil = { git = "https://github.com/rigetti/quil-rust.git", tag = "v0.2.3" }
quil = { git = "https://github.com/rigetti/quil-rust.git", branch = "derive-error-for-evaluation-error" }
num = "0.4.0"
ndarray = "0.15.3"
num-complex = "0.4.0"
Expand Down
69 changes: 53 additions & 16 deletions src/matrix.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,76 @@
use std::collections::HashMap;
use num::complex::Complex64;
use thiserror::Error;

use crate::gates::{gate_matrix, QGate};
use num::complex::Complex64;

use quil::instruction::{Instruction, Qubit};
use quil::expression::EvaluationError;
use crate::matrix::InstructionMatrixError::{InvalidQubit, InvalidInstruction};

pub const C0: Complex64 = Complex64::new(0.0, 0.0);
pub const C1: Complex64 = Complex64::new(1.0, 0.0);
pub const I1: Complex64 = Complex64::new(0.0, 1.0);

pub fn instruction_matrix(instruction: Instruction) -> QGate {
#[derive(Error, Debug)]
pub enum InstructionMatrixError {
#[error("invalid parameter")]
InvalidParameter(#[from] EvaluationError),
#[error("cannot create matrix from gate with variable qubit {0}")]
InvalidQubit(String),
#[error("cannot create matrix from non-gate instruction {0}")]
InvalidInstruction(String),
}

pub fn instruction_matrix(instruction: Instruction) -> Result<QGate, InstructionMatrixError> {
match instruction {
Instruction::Gate {
name,
parameters,
qubits,
modifiers: _,
} => gate_matrix(
name,
parameters
} => {
let params: Result<Vec<f64>, EvaluationError> = parameters
.iter()
.map(|p| {
p.to_owned()
.evaluate_to_complex(&HashMap::new())
.expect("bad")
.re
match p.to_owned().evaluate_to_complex(&HashMap::new()) {
Ok(c) => Ok(c.re),
Err(e) => Err(e),
}
})
.collect(),
qubits
.collect();
let qubits: Result<Vec<_>, _> = qubits
.iter()
.map(|q| match q {
Qubit::Fixed(i) => *i as usize,
Qubit::Variable(_) => todo!(),
Qubit::Fixed(i) => Ok(*i as usize),
Qubit::Variable(q) => Err(InvalidQubit(q.clone())),
})
.collect(),
),
_ => todo!(),
.collect();
Ok(gate_matrix(name, params?, qubits?))
}
instruction => Err(InvalidInstruction(instruction.to_string())),
}
}

#[cfg(test)]
pub mod test {
use crate::matrix::{instruction_matrix, InstructionMatrixError};
use quil::instruction::{Instruction, Qubit};
use crate::gates::QGate;
use quil::expression::Expression;
use quil::expression::Expression::Address;

#[test]
pub fn blah() {
let instruction = Instruction::Gate {
name: "RX".to_string(),
parameters: vec![Expression::Variable("yo".to_string())],
qubits: vec![Qubit::Variable("a".to_string()), Qubit::Fixed(1)],
modifiers: vec![]
};
match instruction_matrix(instruction) {
Ok(_) => {}
Err(e) => println!("{:?}", e)
}
}
}
4 changes: 4 additions & 0 deletions src/vm/errors.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use thiserror::Error;
use crate::matrix::InstructionMatrixError;

#[derive(Error, Debug)]
pub enum VMError {
Expand Down Expand Up @@ -54,6 +55,9 @@ pub enum VMError {
ssource: String,
destination: String,
},

#[error("Error when creating matrix")]
MatrixError(#[from] InstructionMatrixError),
}

// TODO VMError is nice but it doesn't show which instruction caused the error. Define a VMRunError
Expand Down
2 changes: 1 addition & 1 deletion src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ impl VM {

Instruction::Gate { .. } => {
// TODO errors
let matrix = instruction_matrix(instruction);
let matrix = instruction_matrix(instruction)?;
self.apply(&matrix);
}

Expand Down

0 comments on commit 43a01cf

Please sign in to comment.