A QASM interpreter and quantum simulator in Rust.
Make sure you have installed cargo
.
For compiling the WASM version, make sure you have wasm-pack
also installed.
The interpreter golden path is almost complete, although the following is still lacking before version 1.0.0
:
And planned for 1.1.0
is:
Although there is still no support for including external gate definitions,
including qelib1.inc
via the include
instruction will work, for the qelib1.inc
lib is embedded in the simulator:
include "qelib1.inc";
A sample QASM program can be found here:
qasm
OPENQASM 2.0;
include "qelib1.inc";
qreg q[2];
h q[0];
cx q[0], q[1];
The complete specification can be found under the QASM repository.
Install qasmsim
with:
sh
$ cargo install --git https://github.com/delapuente/qasmsim
And simulate a QASM program with:
sh
$ qasmsim source.qasm
See more options with:
``` $ qasmsim --help qasmsim 1.0.0 A QASM interpreter and quantum simulator in Rust.
USAGE: qasmsim [FLAGS] [OPTIONS] [source]
FLAGS: -b, --binary Prints the binary representation of the values -h, --help Prints help information -x, --hexadecimal Prints the hexadecimal representation of the values -i, --integer Prints the interger representation of the values. Default option --probabilities Prints the probabilities vector of the simulation. Ignored if shots is set --statevector Prints the state vector of the simulation. Ignored if shots is set -t, --times Prints times measured for parsing and simulating -V, --version Prints version information -v Verbosity of the output
OPTIONS:
--out
ARGS:
qasmsim
is also a library including a QASM parser which generates a QASM AST,
an interpreter, and a quantum state-vector simulator. The command-line tool is
just one of the multiple consumers the library can have. If you want to install
the library functionality only, remove the default
features when installing:
sh
$ cargo install --no-default-features
You can refer to unit tests (in the files under the src
folder) and integration tests (under the tests
folder) to figure out what is implemented. For passing the tests of the project you can do:
sh
$ cargo test
qasmsim
can be used in the web if you compile it for Web Assembly. Doing it is easy, simply download the sources, ensure you have wasm-pack
installed and run:
sh
$ wasm-pack build
It will compile your project and pack it inside the pkg
folder. Now enter the www
directory, install the dependencies with (you only need run this once):
sh
$ npm install
And start the web server with:
sh
$ npm start
Browse the URL provided by the second command and you'll see a blank page. Go to the developer tools of your browser and try running a small test:
js
var result = qasmsim.run(`
OPENQASM 2.0;
include "qelib1.inc";
qreg q[2];
h q[0];
cx q[0], q[1];
`);
The module is exported by default as the qasmsim
object in window
and implments the following interface:
```ts interface qasmsim { run: (input: string, shots?: number) => Execution }
interface Execution { histogram?: Histogram, probabilities: Float64Array, statevector: Float64Array, memory: Memory, times: ExecutionTimes }
type Memory = { [key: string]: Array[number] } type Histogram = { [key: string]: Array[[number, number]] } type ExecutionTimes = { parsingtime: number, simulationtime: number, serialization_time: number } ```