Warning
|
Work in Progress |
use quamputer::computer::QuantumComputer;
use quamputer::common_gate::CommonGate::{CNot, Hadamard};
fn main() -> Result<(), String> {
let computer = QuantumComputer::new(3);
let circuit = computer.new_circuit_builder()
.add_operation(Hadamard(0))
.add_operation(CNot(1, [0]))
.add_operation(CNot(2, [1]))
.build()?;
let initial_state = computer.zero_state();
let result = circuit.execute(&initial_state);
println!("{:?}", result.current_state());
Ok(())
}
This will print:
input : State 3 qbits : (1.000000,0.000000)x|000>
output : State 3 qbits : (0.707107,0.000000)x|000> (0.707107,0.000000)x|111>
Three types of elements can be added to a circuit:
-
Gate: the base operation on a small set of qbit (like Hadamard or Swap)
-
Measure : A measurement made on a qbit
-
Loop : a circuit and a stop condition
The framework contains two types of gates: BaseGate
and Gate
.
A BaseGate
does not have any control qbits. A Gate
is built by adding control qbits (or not) to a BaseGate
.
The available variant of BaseGate
are:
-
Not
-
X
-
Y
-
Z
-
Hadamard
-
Swap
BaseGate has the Into<Gate> trait that converts it into a Gate
without control qbits. For instance:
use quamputer::base_gate::BaseGate::{Hadamard};
fn main() -> Result<(), String> {
let gate = Hadamard(1);
Ok(())
}
is equivalent (when used as a Gate
) to:
use quamputer::base_gate::BaseGate::{Hadamard};
use quamputer::operation::CircuitElement::Gate;
fn main() -> Result<(), String> {
let gate = Gate::new(Hadamard(1),vec![]);
Ok(())
}
Below are two ways to create a CNot gate on qbit 0 controlled by qbit 1:
use quamputer::base_gate::BaseGate::{Hadamard,Not};
use quamputer::operation::CircuitElement::Gate;
fn main() -> Result<(), String> {
let cnot1 = Gate::new(Not(0),vec![1]);
let cnot2 = Not(0).with_one_control(1);
Ok(())
}
CNot, CSwap, Toffoli (CCNot), Fredkin (CSwap) are common gates and as such an enum CommonGate
with the trait Into<Gate> defines those. For instance a Fredkin can be defined with:
use quamputer::common_gate::CommonGate::Fredkin;
fn main() -> Result<(), String> {
let gate = Fredkin(0,1,[2]);
Ok(())
}
use quamputer::common_gate::CommonGate::{CNot, Hadamard};
use quamputer::computer::QuantumComputer;
use quamputer::condition::StopCondition;
fn main() -> Result<(), String> {
let computer = QuantumComputer::new(3);
let sub_circuit = computer.new_circuit_builder()
.add_operation(Hadamard(0))
.add_operation(CNot(1, [0]))
.add_operation(CNot(2, [1]))
.add_measure("q0", 1)
.build()?;
let circuit = computer.new_circuit_builder()
.add_loop(sub_circuit,StopCondition::MaxZeroSampling {id:"q0".to_string(),nb:10})
.build()?;
for i in 1..5 {
let initial_state = computer.zero_state();
let result = circuit.execute(&initial_state);
println!("-- Iteration {} --",i);
println!("input : {:?}", initial_state);
println!("output : {:?}", result.current_state());
println!("q0 measures : {:?}", result.get_count("q0").unwrap());
}
Ok(())
}
-- Iteration 1 --
input : State 3 qbits : (1.000000,0.000000)x|000>
output : State 3 qbits : (1.000000,0.000000)x|100>
q0 measures : MeasureCount { nb_zero: 10, nb_one: 14 }
-- Iteration 2 --
input : State 3 qbits : (1.000000,0.000000)x|000>
output : State 3 qbits : (1.000000,0.000000)x|100>
q0 measures : MeasureCount { nb_zero: 10, nb_one: 12 }
-- Iteration 3 --
input : State 3 qbits : (1.000000,0.000000)x|000>
output : State 3 qbits : (1.000000,0.000000)x|100>
q0 measures : MeasureCount { nb_zero: 10, nb_one: 6 }
-- Iteration 4 --
input : State 3 qbits : (1.000000,0.000000)x|000>
output : State 3 qbits : (1.000000,0.000000)x|001>
q0 measures : MeasureCount { nb_zero: 10, nb_one: 9 }