use crate::gates::Gate;
use crate::stabilizer::PauliOp;
pub struct I
{
}
impl I
{
pub fn new() -> Self
{
I { }
}
}
impl crate::gates::Gate for I
{
fn cost(&self) -> f64
{
0.0
}
fn description(&self) -> &str
{
"I"
}
fn nr_affected_bits(&self) -> usize
{
1
}
fn matrix(&self) -> crate::cmatrix::CMatrix
{
crate::cmatrix::CMatrix::eye(2)
}
fn apply_slice(&self, _state: crate::cmatrix::CVecSliceMut)
{
}
fn is_stabilizer(&self) -> bool
{
true
}
fn conjugate(&self, _ops: &mut [PauliOp]) -> crate::error::Result<bool>
{
Ok(false)
}
}
impl crate::export::OpenQasm for I
{
fn open_qasm(&self, bit_names: &[String], bits: &[usize])
-> crate::error::Result<String>
{
Ok(format!("id {}", bit_names[bits[0]]))
}
}
impl crate::export::CQasm for I
{
fn c_qasm(&self, bit_names: &[String], bits: &[usize])
-> crate::error::Result<String>
{
Ok(format!("i {}", bit_names[bits[0]]))
}
}
impl crate::export::Latex for I
{
fn latex(&self, bits: &[usize], state: &mut crate::export::LatexExportState)
-> crate::error::Result<()>
{
self.check_nr_bits(bits.len())?;
state.set_field(bits[0], String::from(r"\qw"))
}
}
#[cfg(test)]
mod tests
{
use super::I;
use crate::export::{Latex, LatexExportState, OpenQasm, CQasm};
use crate::gates::{gate_test, Gate};
use crate::stabilizer::PauliOp;
#[test]
fn test_description()
{
let gate = I::new();
assert_eq!(gate.description(), "I");
}
#[test]
fn test_nr_affected_bits()
{
let gate = I::new();
assert_eq!(gate.nr_affected_bits(), 1);
}
#[test]
fn test_cost()
{
let gate = I::new();
assert_eq!(gate.cost(), 0.0);
}
#[test]
fn test_matrix()
{
let z = crate::cmatrix::COMPLEX_ZERO;
let o = crate::cmatrix::COMPLEX_ONE;
let i = I::new();
assert_complex_matrix_eq!(i.matrix(), array![[o, z], [z, o]]);
}
#[test]
fn test_apply()
{
let z = crate::cmatrix::COMPLEX_ZERO;
let o = crate::cmatrix::COMPLEX_ONE;
let x = crate::cmatrix::COMPLEX_HSQRT2;
let mut state = array![[o, z, x, x], [z, o, x, -x]];
let result = array![[o, z, x, x], [z, o, x, -x]];
gate_test(I::new(), &mut state, &result);
}
#[test]
fn test_open_qasm()
{
let bit_names = [String::from("qb")];
let qasm = I::new().open_qasm(&bit_names, &[0]);
assert_eq!(qasm, Ok(String::from("id qb")));
}
#[test]
fn test_c_qasm()
{
let bit_names = [String::from("qb")];
let qasm = I::new().c_qasm(&bit_names, &[0]);
assert_eq!(qasm, Ok(String::from("i qb")));
}
#[test]
fn test_latex()
{
let gate = I::new();
let mut state = LatexExportState::new(1, 0);
assert_eq!(gate.latex(&[0], &mut state), Ok(()));
assert_eq!(state.code(),
r#"\Qcircuit @C=1em @R=.7em {
\lstick{\ket{0}} & \qw & \qw \\
}
"#);
}
#[test]
fn test_conjugate()
{
let mut ops = [PauliOp::X];
assert_eq!(I::new().conjugate(&mut ops), Ok(false));
assert_eq!(ops, [PauliOp::X]);
}
}