Crate spinoza

source ·
Expand description

A High Performance Quantum State Simulator

Spinoza is a fast and flexible quantum simulator written exclusively in Rust, with bindings available for Python users. Spinoza simulates the evolution of a quantum system’s state by applying quantum gates, with the core design principle being that a single-qubit gate applied to a target qubit preserves the probability of pairs of amplitudes corresponding to measurement outcomes that differ only in the target qubit. Spinoza is intended to enable the development of quantum computing solutions by offering researchers and quantum developers a simple, flexible, and fast tool for classical simulation.

How to use Spinoza

There are three ways to use Spinoza:

  • Functional the simplest way to mutate a quantum state, directly.
    • apply for quantum transformations that do not need a control (qubit).
    • c_apply for quantum transformations that have a single control.
    • mc_apply for quantum transformation that require multiple controls.
  • Object Oriented the QuantumCircuit represents a quantum circuit. Using the QuantumCircuit one can create, mutate, and simulate quantum circuits with various quantum gates, operators, etc.
  • Python Bindings Spinoza has python bindings named Spynoza
    • All functionality for QuantumCircuit and other functions have corresponding bindings created using PyO3.

Examples

Let’s encode the value, 2.4 using the three aforementioned approaches:

Functional

use spinoza::{
    core::{iqft, State},
    gates::{apply, Gate},
    math::{pow2f, PI},
    utils::{to_table},
};

pub fn main() {
    let n = 3;
    let v = 2.4;
    let mut state = State::new(n);

    for i in 0..n {
        apply(Gate::H, &mut state, i);
    }
   for i in 0..n {
        apply(Gate::P(2.0 * PI / (pow2f(i + 1)) * v), &mut state, i);
    }
    let targets: Vec<usize> = (0..n).rev().collect();

    iqft(&mut state, &targets);
    println!("{}", to_table(&state));
}

Object Oriented (OO)

use spinoza::{
    core::{iqft, State},
    circuit::{QuantumCircuit, QuantumRegister},
    math::{pow2f, PI},
    utils::{to_table},
};

pub fn main() {
    let n = 3;
    let v = 2.4;
    let now = std::time::Instant::now();
    let mut q = QuantumRegister::new(n);
    let mut qc = QuantumCircuit::new(&mut [&mut q]);

    for i in 0..n {
        qc.h(i)
    }
    for i in 0..n {
        qc.p(2.0 * PI / pow2f(i + 1) * v, i)
    }

    let targets: Vec<usize> = (0..n).rev().collect();
    qc.iqft(&targets);
    qc.execute();
    println!("{}", to_table(qc.get_statevector()));
}

Spynoza

from math import pi
from spynoza import QuantumCircuit, QuantumRegister, show_table


def value_encoding(n, v):
    q = QuantumRegister(n)
    qc = QuantumCircuit(q)

    for i in range(n):
        qc.h(i)

    for i in range(n):
        qc.p(2 * pi / (2 ** (i + 1)) * v, i)

    qc.iqft(range(n)[::-1])

    qc.execute()
    return qc.get_statevector()


if __name__ == "__main__":
    state = value_encoding(4, 2.4)
    print(show_table(state))

More complex examples can be found in the Spinoza examples and the Spynoza exmaples.

References

@misc{yusufov2023designing,
      title={Designing a Fast and Flexible Quantum State Simulator},
      author={Saveliy Yusufov and Charlee Stefanski and Constantin Gonciulea},
      year={2023},
      eprint={2303.01493},
      archivePrefix={arXiv},
      primaryClass={quant-ph}
}

Modules

  • Abstractions for a quantum circuit
  • Configuration options for running spinoza
  • Consts for Quantum State Simulation, such as the Pauli Gates and the Hadamard gate
  • Abstractions for representing a Quantum State
  • Abstractions for quantum logic gates
  • An assortment of mathematical structures, functions, and constants for quantum state simulation.
  • Functionality for measurement
  • Functionality for creating a QuantumCircuit from an OpenQASM 2.0 program
  • Functionality for applying large 2^n * 2^n matrices to the state Ideally, this should be a last resort
  • An assortment of utility functions for visualizing, benchmarking, and testing.