Skip to main content

A quantum computer state vector/stabilizer circuit simulator and assembly language

Project description

qsy

A quantum computer state vector/stabilizer circuit simulator and assembly language.

Table of Contents

Installation

$ pip install qsy

This will install the Python library qsy and command-line tool qsyasm.

qsy

qsy is a Python library for simulating quantum circuits.

Example

The following code creates an entangled state and prints its state vector in Dirac notation.

from qsy import QuantumRegister, gates

qr = QuantumRegister(2)
qr.apply_gate(gates.H, 0)
qr.apply_gate(gates.CX, 0, 1)

print(qr.to_dirac())

The output will be:

+0.70711|00> +0.70711|11>

qsyASM

qsyASM is a quantum assembly language acting as front-end for qsy. It allows you to quickly write and debug quantum programs. It also allows for efficient simulation of stabilizer circuits (a quantum circuit consisting solely of CNOT, Hadamard, and phase gates) using the chp back-end.

Usage

usage: qsyasm [-h] [-V] [-v] [-t] [-b B] [-s SHOTS] [--ignore-print-warning]
              filename

qsyasm assembly runner

positional arguments:
  filename              qsyasm file to execute

optional arguments:
  -h, --help            show this help message and exit
  -V, --version         show program's version number and exit
  -v, --verbose         verbose output
  -t, --time            time program execution
  -b B, --backend B     simulator back-end to use: chp or statevector
                        (default: statevector)
  -s SHOTS, --shots SHOTS
                        amount of shots to run
  --ignore-print-warning
                        ignore register too large to print warning

Example

The following qsyASM program creates an entangled state and measures to a classical register:

qreg[2] q
creg[2] c

h q[0]
cx q[0], q[1]

meas q, c

Running it:

$ qsyasm examples/qsyasm/bell.qs
q[2]: +1|11>
      +0 | 00
      +0 | 01
      +0 | 10
      +1 | 11
c[2]: 11

Or running it a number of times:

$ qsyasm examples/qsyasm/bell.qs --shots=1024
q[2]: +1|00>
      +1 | 00
      +0 | 01
      +0 | 10
      +0 | 11
c[2]: {'11': 550, '00': 474}

More examples such as the quantum phase estimation algorithm can be found in the examples/qsyasm folder.

Syntax

The structure of a qsyASM program consists of a list of instructions. An instruction is defined as an operation followed by its arguments.

Operations

The instruction

cx q[0], q[1]

applies a CNOT operation with control qubit q[0] and target qubit q[1]. Some operations take an angle (in radians) as argument. The parameterized operation

rz(pi/2) q[0]

rotates q[0] π/2 radians around the Z axis. Expressions are allowed in parameterized operations. Expression operators supported are +, -, *, / and ** (power). The variable pi is available for convenience.

Adjoint Operation

To apply the adjoint of a gate, the adj keyword is available. For example, to apply the adjoint of S (S dagger):

adj s q[0]
List of Operations
Gate qsyASM operation
Pauli I i target
Pauli X x target
Pauli Y y target
Pauli Z z target
Hadamard h target
S s target
T t target
Rx rx(angle) target
Ry ry(angle) target
Rz rz(angle) target
CNOT cx control, target
CZ cz control, target
CRx crx(angle) control, target
CRy cry(angle) control, target
CRz crz(angle) control, target
Toffoli ccx controlA, controlB, target

Note: The operations CNOT, H, S, X, Z and CZ can be efficiently simulated with the CHP back-end. Using any other operations with the CHP back-end will result in an error.

Registers

Defining a quantum register is done with the qreg operation. The instruction

qreg[5] q

defines a 5 qubit quantum register named q. Likewise, a classical register (useful for measuring) can be defined as

creg[5] c

Qubits in a quantum register are initiated to |0⟩, and bits in a classical register to 0.

Measurement

Measurement can be done on individual qubits, or a complete quantum state. The program

qreg[5] q
creg[1] c

h q[0]

meas q[0], c[0]

measures q[0] to c[0], collapsing the state and storing the result in c[0]. The measurement result can be ignored by only passing one argument to meas:

meas q[0]

To measure a complete quantum state you can pass the whole quantum and classical register:

qreg[3] q
creg[3] c

; 3 qubit GHZ state
h q[0]
cx q[0], q[1]
cx q[0], q[2]

meas q, c

collapsing the quantum register q and storing the measurement result in c. This only works when the quantum register and classical register are equal in size.

License

This project is licensed under the MIT License. See the LICENSE file for the full license.

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

qsy-0.4.tar.gz (17.2 kB view hashes)

Uploaded Source

Supported by

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