traits.rs 812 B

123456789101112131415161718192021222324252627
  1. use failure::Error;
  2. use gate::Gate;
  3. use std::fmt::{Debug, Display};
  4. pub trait Backend: Debug + Display {
  5. fn num_qubits(&self) -> u8;
  6. fn apply_gate(&mut self, gate: Gate, target: u8) -> Result<(), Error>;
  7. fn apply_controlled_gate(&mut self, gate: Gate, control: u8, target: u8) -> Result<(), Error>;
  8. fn measure_qubit(&mut self, target: u8) -> Result<u64, Error>;
  9. fn measure(&mut self) -> Result<u64, Error> {
  10. let mut result = 0;
  11. for i in 0..self.num_qubits() {
  12. let bit_mask = 1 << i;
  13. if self.measure_qubit(i)? == 1 {
  14. // 1, set the bit in result
  15. result |= bit_mask
  16. } else {
  17. // 0, clear the bit in result
  18. result &= !bit_mask
  19. }
  20. }
  21. Ok(result)
  22. }
  23. }