Skip to content

Commit

Permalink
Making InstructionType hold specific trait
Browse files Browse the repository at this point in the history
harder than I thought it'd be...

Main difficulty lies in testing due to trait objects not allowed
PartialEq to be implemented which screws up testing. Hopefully I'll get
this figured out in the morning...
  • Loading branch information
Haadi-Khan committed Jul 19, 2024
1 parent 1bc76ba commit 51bf187
Show file tree
Hide file tree
Showing 4 changed files with 261 additions and 230 deletions.
2 changes: 1 addition & 1 deletion src/circuit_instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::bit::{Qubit, Clbit};

/// Description of a Qiskit Circuit element. Provides a specific operation and the
/// qubits/classical bits it interacts with.
#[derive(Debug, PartialEq, Clone)]
#[derive(Debug)]
pub struct CircuitInstruction {
operation: InstructionType,
qubits: Vec<Qubit>,
Expand Down
4 changes: 2 additions & 2 deletions src/gates/singleton.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl Gate for XGate {

#[derive(Debug, PartialEq, Clone, Operation)]
/// A Pauli-Y gate
struct YGate {
pub struct YGate {
instruction: Instruction,
}

Expand Down Expand Up @@ -64,7 +64,7 @@ impl Gate for YGate {

#[derive(Debug, PartialEq, Clone, Operation)]
/// A Pauli-Z gate
struct ZGate {
pub struct ZGate {
instruction: Instruction,
}

Expand Down
60 changes: 44 additions & 16 deletions src/instruction.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
// use core::any::Any;

use ndarray::Array2;
use numpy::Complex64;
use std::fmt::Debug;

use crate::gates::singleton::{HadamardGate, XGate, YGate, ZGate};

/// An instruction is defined by a name, the number of qubits/classical bits it
/// acts on, and other optional parameters.
///
Expand All @@ -20,7 +24,7 @@ pub struct Instruction {

/// Operations are the most general trait for quantum operations. They provide
/// basic methods to retrieve information from an instruction.
pub trait Operation: Debug + PartialEq + Clone {
pub trait Operation: Debug {
fn name(&self) -> &str;
fn num_qubits(&self) -> usize;
fn num_clbits(&self) -> usize;
Expand All @@ -32,14 +36,14 @@ pub trait Operation: Debug + PartialEq + Clone {

/// Contains the different types of instructions that can be used in a quantum
/// circuit.
#[derive(Debug, PartialEq, Clone)]
#[derive(Debug)]
pub enum InstructionType {
Gate(Instruction),
Measurement(Instruction),
Reset(Instruction),
Barrier(Instruction),
Delay(Instruction),
Store(Instruction),
Gate(Box<dyn Gate>),
Measurement(Box<dyn Measurement>),
Reset(Box<dyn Reset>),
Barrier(Box<dyn Barrier>),
Delay(Box<dyn Delay>),
Store(Box<dyn Store>),
}

/// Gates are unitary operations that act on qubits. They can be converted into
Expand Down Expand Up @@ -117,23 +121,47 @@ impl Operation for Instruction {
}

impl InstructionType {
/// TODO: Add measurement, reset, barrier, delay, and store
pub fn from(instruction: Instruction) -> InstructionType {
// convert from the instruction name to the enum variant
let allowed_gates: Vec<&str> = vec!["x", "y", "z", "h", "cx"];

let name = instruction.name.as_str();

match name {
name if allowed_gates.contains(&name) => InstructionType::Gate(Self::to_gate(name)),
// "measure" => InstructionType::Measurement(instruction),
// "reset" => InstructionType::Reset(instruction),
// "barrier" => InstructionType::Barrier(instruction),
// "delay" => InstructionType::Delay(instruction),
// "store" => InstructionType::Store(instruction),
_ => unimplemented!(),
}
}

/// TODO: Update as more gates are added
fn to_gate(name: &str) -> Box<dyn Gate> {
match name {
name if allowed_gates.contains(&name) => InstructionType::Gate(instruction),
"measure" => InstructionType::Measurement(instruction),
"reset" => InstructionType::Reset(instruction),
"barrier" => InstructionType::Barrier(instruction),
"delay" => InstructionType::Delay(instruction),
"store" => InstructionType::Store(instruction),
"x" => Box::new(XGate::new()),
"y" => Box::new(YGate::new()),
"z" => Box::new(ZGate::new()),
"h" => Box::new(HadamardGate::new()),
// "cx" => Box::new(CXGate::new()),
_ => unimplemented!(),
}
}

// TODO: Add the rest of the main operation methods
}

// impl<T: PartialEq + Operation> PartialEq<T> for InstructionType {
// fn eq(&self, other: &T) -> bool {
// if let Some(this) = self.as_any().downcast_ref::<T>() {
// this == other
// } else {
// false
// }
// }
// }

impl Operation for InstructionType {
fn name(&self) -> &str {
match self {
Expand Down
Loading

0 comments on commit 51bf187

Please sign in to comment.