Add this lines to your Cargo.toml file to use QVNT crate:
toml
[dependencies]
qvnt = { git = "https://github.com/MucTepDayH16/qvnt", branch = "qasm_interpreter", features = ["interpreter"] }
Quantum register and operators are controlled by bitmasks. Each bit in it will act on a specific qubit.
```rust use qvnt::prelude::*;
// Create quantum register with 10 qubits let mut qreg = QReg::new(10); // or with initial state, where 5th, 6th and 7th qubits are already in state |1>. let mut qreg = QReg::new(10).init_state(0b0011100000);
// Create qft (Quantum Fourier Transform) operation, acting on first 5 qubits in q_reg. let op = op::qft(0b0000011111);
// Apply created operation q_reg.apply(&op);
// Measure and write first 3 qubit, which leads to collapse of qreg wave function. // Measured variable will contain one of the following values: // 0b000, 0b001, 0b010, 0b011, 0b100, 0b101, 0b110, 0b111 let measured = qreg.measure_mask(0b0000000111); ```
You're able to use VReg to simplify operations definition:
```rust use qvnt::prelude::*;
let mut qreg = QReg::new(10); let q = qreg.get_vreg();
// Crate Hadamard operator, that act on odd qubits. let op = op::h(q[1] | q[3] | q[5] | q[7] | q[9]); // This is equivalent to op::h(0b0101010101); ```
ALL operators have inverse versions, accessing by .dgr()
method:
```rust
use qvnt::prelude::*;
let usualop = op::s(0b1); // Inverse S operator let inverseop = op::s(0b1).dgr(); ```
Also, ALL these operators could be turned into controlled ones, using .c(...)
method:
```rust
use qvnt::prelude::*;
let usualop = op::x(0b001);
// NOT gate, controlled by 2 qubits, aka CCNOT gate, aka Toffoli gate
let controlledop = op::x(0b001).c(0b110).unwrap();
Controlled operation has to be unwrapped, since it could be None if its mask overlaps with the mask of operator.
For example, this code will *panic*:
rust,should_panic,panics
use qvnt::prelude::*;
let _ = op::x(0b001).c(0b001).unwrap();
```
It is REPL interpreter, that could be used to process quantum operation without compiling code.
shell
cargo install qvnt-i
Now, you are able to 'run' quantum simulator with OpenQASM language.
*.qasm
files should be passed to interpreter via cli:
shell
qvnt-i --input ./cirquit.qasm
|Q> :go
or via interpreter:
shell
qvnt-i
|Q> :load ./cirquit.qasm
|Q> :go
Another way of running simulator is writing cirquit on OpenQASM language directly in REPL:
shell
qvnt-i
|Q> qreg q[4];
|Q> creg c[4];
|Q> h q;
|Q> measure q -> c;
|Q> :go
|Q> :class
* :go
- process the simulation;
* :class
- acquire the result from classical register.
REPL is lazy: it only starts computation, if it encounters :go
.
This example will shows the single number every time:
shell
|Q> qreg q[4];
|Q> creg c[4];
|Q> h q;
|Q> measure q -> c;
|Q> :go
|Q> :class
|Q> :class
|Q> :class
|Q> :class
...
Unlike that, repeating :go
will proceed with different result every time:
shell
|Q> qreg q[4];
|Q> creg c[4];
|Q> h q;
|Q> measure q -> c;
|Q> :go
|Q> :class
|Q> :go
|Q> :class
|Q> :go
|Q> :class
...
All commands should be preceeded with :
.
Otherwise, REPL considers to parse line as OpenQASM source.
The full list of commands:
ignore
loop N Repeat following commands N time
tags TAG Create TAG with current state
goto TAG Swap current state to TAG's state
class Show state of classical registers
polar Show state of quantum registers in polar form
prob Show state of quantum registers in probability form
ops Snow current quantum operations queue
go Start modulating quantum computer
reset Clear current state
names Show aliases for quantum and classical bits
load FILE Load state from FILE according to QASM language script
help Show this reference
quit Exit interpreter
Licensed under MIT License