Skip to main content

A minimal framework to simulate quantum computation using matrix and tensor maths.

Project description

Tiny-Q

Tiny-Q is a minimal framework to show quantum computation basics in a tensor/matrix perspective view.

demo

Install

⚪ From PyPI

ℹ Due to the name conflict with an existing pypi package, you have to address TinyQSim rather than TinyQ :(

  • pip install TinyQSim

⚪ From source

  • git clone https://github.com/Kahsolt/Tiny-Q
  • cd Tiny-Q
  • pip install .
  • you can see exmaples or run run_unnittest.cmd to verify installation

Features

⚪ Tiny-Q interactive playground

Start interactive python shell with Tiny-Q environment: python -m tiny_q

(InteractiveConsole)
>>> v('00')
array([1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j], dtype=complex64)
>>> H
array([[ 0.7071+0.j,  0.7071+0.j],
       [ 0.7071+0.j, -0.7071+0.j]], dtype=complex64)
>>> CNOT
array([[1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
       [0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j],
       [0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j],
       [0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j]], dtype=complex64)
>>> q = CNOT * (H @ I) | v('00')
>>> q.info()
|phi>
  state: [0.7071+0.j 0.    +0.j 0.    +0.j 0.7071+0.j]
  amp: [0.7071 0.     0.     0.7071]
  prob: [0.5 0.  0.  0.5]
  density: [[0.5+0.j 0. +0.j 0. +0.j 0.5+0.j]
 [0. +0.j 0. +0.j 0. +0.j 0. +0.j]
 [0. +0.j 0. +0.j 0. +0.j 0. +0.j]
 [0.5+0.j 0. +0.j 0. +0.j 0.5+0.j]]
  trace: (0.99999994+0j)

>>> q > Measure()
{'00': 489, '01': 0, '10': 0, '11': 511}
>>> q < Measure
>>> q.info()
|phi>
  state: [1.+0.j 0.+0.j 0.+0.j 0.+0.j]
  amp: [1. 0. 0. 0.]
  prob: [1. 0. 0. 0.]
  density: [[1.+0.j 0.+0.j 0.+0.j 0.+0.j]
 [0.+0.j 0.+0.j 0.+0.j 0.+0.j]
 [0.+0.j 0.+0.j 0.+0.j 0.+0.j]
 [0.+0.j 0.+0.j 0.+0.j 0.+0.j]]
  trace: (1+0j)

>>> Ctrl+D
now exiting InteractiveConsole...

⚪ Tiny-Q syntax / notations

Tiny-Q strictly distinguishes the following four categories of operations, an re-assigin each to a different python operator, making expresssions syntax formula-like thus more clear :)

  • system expansion (@) is the hadamard product
  • gate composition (*) and gate construction (<<) are the matrix product
  • gate application (|) is the quantum state evolution
  • virtual measure (>) and real measure (<) are the quantum measurements
# use matmul @ for system expansion (gate/state tensor product)
# u = Gate @ Gate
u = H @ I
# q = State @ State
q = v0 @ v1
# NOTE: the low qubit starts from **right**
q = v_high @ v_mid @ v_low    # => |high,mid,low>

# use mul * for gate composition
# u = Gate * Gate (more like math formula, reducing from **right**)
u = X * H
u = gate3 * gate2 * gate1
# use lshift << for gate construction
# u = Gate << Gate (more like programs, running from **left**, modify **inplace**)
u = H << X
u = gate1 << gate2 << gate3

# use pipe | for gate application
# q = Gate | State
q = X | v0
# use pipe | for pauli expectation or state fidelity
# E = State | Gate | State
E = State.rand() | (X @ Z) | State.rand()
# fid = State | State
fid = (X | v('01')) | (H | v('01'))

# use > for virtual measure, the state does not really change
# r = State > Measure, single shot
r = H | v0 > Measure
# r = State > Measure(count), multi shots
r = CNOT * (H @ I) | State.zero(2) > Measure(1000)
# p = State > State, project by state
p = v0 > h0
# p = State > MeasureOp, project by measure operator
p = h0 > M0

# use < for real measure, the state will collapse
# State < Measure
q = CNOT * (H @ I) | v('00')
q < Measure

API stub

class Meta:
  .n_qubits -> int          # qubit count of current system
  .dagger -> Meta           # dagger of State/Gate/MeasureOp

class State(Meta):
  .zero() -> State          # alloc a |0> string
  .one() -> State           # alloc a |1> string
  .rand() -> State          # get a random state (usually as test stub)
  .__eq__() -> bool         # state equality (ignoring global phase)
  .__matmul__() -> State    # v0 @ v1: state expansion
  .__lt__() -> Union        # v0 < Measure, real measure with state collapse
  .__gt__() -> Union        # v0 > Measure|Measure()|State|MeasureOp, virtual measurements
  .is_pure -> bool          # purity
  .amp -> ndarray           # amplitude
  .prob -> ndarray          # probabilty distribution
  .density -> ndarray       # density matrix
  .trace -> float           # trace of density matrix
  .info()                   # quick show info
  .plot_prob()              # plot probabilty distribution
  .plot_density()           # plot density matrix
  .plots()                  # plot all figures

class Gate(Meta):
  .__eq__() -> bool         # gate equality
  .__neg__() -> Gate        # -H, global negative
  .__xor__() -> Gate        # H^alpha, gate self-power
  .__mul__() -> Gate        # X * H: gate composition
  .__lshift__() -> Gate     # H << X: gate composition (reverse order of __mul__)
  .__matmul__() -> Gate     # X @ H: gate expansion
  .__or__() -> State        # X | v0: gate application
  .is_unitary -> bool       # unitary (should always be True)
  .is_hermitian -> bool     # hermitian (True for most gates)
  .info()                   # quick show info

class MeasureOp(Meta):
  .check_completeness() -> bool

by Armit 2023/03/15

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

TinyQSim-1.1.tar.gz (14.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

TinyQSim-1.1-py2.py3-none-any.whl (13.4 kB view details)

Uploaded Python 2Python 3

File details

Details for the file TinyQSim-1.1.tar.gz.

File metadata

  • Download URL: TinyQSim-1.1.tar.gz
  • Upload date:
  • Size: 14.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.19

File hashes

Hashes for TinyQSim-1.1.tar.gz
Algorithm Hash digest
SHA256 ffa5fc91736b6431e08ee5ffdb78b84be65ff06d766ef998eac0e9e9ccac5307
MD5 ec800f504c4a06660eea2dd1ac1b5efe
BLAKE2b-256 32d165b3bbca9aa088fc3713d9ed072dd8c932656de6c78ee315756d2b51a93c

See more details on using hashes here.

File details

Details for the file TinyQSim-1.1-py2.py3-none-any.whl.

File metadata

  • Download URL: TinyQSim-1.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 13.4 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.19

File hashes

Hashes for TinyQSim-1.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 962b736664ac5265b755f89b80bd77c8d671a6157d3f62074b95265bd83320cd
MD5 6aa8584d92fa8912d5589c480c0a0e2d
BLAKE2b-256 397600795e6a759e1aa9259d315aaa95b9e3fd9a9bddb07bf27de1611977266a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page