Skip to main content

Symbolic Calculation of Quantum Computation with Sympy

Project description

plot

Qympy - Quantum Analytic Computation with Sympy

A sympy based python package for symbolic calculation of quantum circuit and machine learning. See GitHub: https://github.com/r08222011/Qympy


Installation

Simply run pip install, see Qympy

pip install qympy

Get Started

See qympy/example/example_circuit.ipynb

1. Circuit Initialization Common circuits ansatz can be found in qympy.quantum_circuit, mostly follow with Qiskit.operations. To build a circuit from beginning, use qympy.quantum_circuit.sp_circuit.Circuit. The basic use of Circuit is same as Qiskit. For example:

from qympy.quantum_circuit.sp_circuit import Circuit

qc = Circuit(3)   # initialize a 3-qubit quantum circuit
qc.h(0)           # Hadamard gate on 0th qubit
qc.ry("x", 0)     # y-rotation on 0th qubit with theta = x
qc.rxx("y", 1, 2) # xx-rotation on 1st and 2nd qubits with theta = y
qc.cx(0,1)        # CNOT on 1st and 2nd qubits
qc.cz(1,2)        # CZ on 1st and 2nd qubits

2. Draw the circuit We now have initialized a quantum circuit. To see the circuit we built, we can use Circuit.draw(). This method use qiskit.circuit.QuantumCircuit.draw with draw('mpl') as default. For example:

qc.draw("mpl")

plot

3. Evolve and measure the circuit The last step for getting the analytic expression is to call the method Circuit.evolve(). This will calculate the final state with the gates applied. After evolving the quantum state, we can measure the quantum state with X, Y, Z basis with a single certain qubit. For example:

'''It would be a good habit to evolve the state first.
Although when using 'measure' qympy will automatically evolve if you haven't evolve.
We design in this way since we won't always need to know the final state for every case.'''

qc.evolve() # evolve the circuit
result = qc.measure(2, "Z") # measure the 2nd qubit in Z-basis

The result would be

                           2                                    2             
  ⎛        ⎛x⎞         ⎛x⎞⎞            ⎛        ⎛x⎞         ⎛x⎞⎞            ⎛ 
  ⎜  √2⋅sin⎜─⎟   √2⋅cos⎜─⎟⎟            ⎜  √2⋅sin⎜─⎟   √2⋅cos⎜─⎟⎟            ⎜√
  ⎜        ⎝2⎠         ⎝2⎠⎟     2⎛y⎞   ⎜        ⎝2⎠         ⎝2⎠⎟     2⎛y⎞   ⎜ 
- ⎜- ───────── + ─────────⎟ ⋅sin ⎜─⎟ + ⎜- ───────── + ─────────⎟ ⋅cos ⎜─⎟ - ⎜─
  ⎝      2           2    ⎠      ⎝2⎠   ⎝      2           2    ⎠      ⎝2⎠   ⎝ 

                     2                                  2        
     ⎛x⎞         ⎛x⎞⎞            ⎛      ⎛x⎞         ⎛x⎞⎞         
2⋅sin⎜─⎟   √2⋅cos⎜─⎟⎟            ⎜√2⋅sin⎜─⎟   √2⋅cos⎜─⎟⎟         
     ⎝2⎠         ⎝2⎠⎟     2⎛y⎞   ⎜      ⎝2⎠         ⎝2⎠⎟     2⎛y⎞
──────── + ─────────⎟ ⋅sin ⎜─⎟ + ⎜───────── + ─────────⎟ ⋅cos ⎜─⎟
   2           2    ⎠      ⎝2⎠   ⎝    2           2    ⎠      ⎝2⎠

Quantum Machine Learning

See qympy/example/example_ml.ipynb

In this section, we demonstrate how to use symbolic expression to calculate machine learning, including classical and quantum machine learning, also hybrid. We use a very simple hybrid model with 2-dimensional input data for example.

1. Contruct a hybrid model We construct a hybrid model with a Linear layer followed by a quantum circuit, which is constructed with AngleEncoding and SingleRot, and finally end up with a Measurement.

import sympy as sp
from qympy.quantum_circuit.sp_circuit import Circuit
from qympy.machine_learning.classical import Linear
from qympy.machine_learning.quantum import Measurement, AngleEncoding, SingleRot

class HybridModel:
    def __init__(self, input_dim):
        self.net = [
            Linear(input_dim, input_dim),
            AngleEncoding(input_dim, rot_gate="ry") + SingleRot(input_dim, rot_mode=['rz'], ent_mode='cx'),
            Measurement(qubits=[0], bases=["Z"]),
        ]
    def __call__(self, x):
        for submodel in self.net:
            x = submodel(x)
        return x

2. Feed forward the input data We then feed forward a 2-dimensional data x = [x0, x1]

# initialize input variables: x0 and x1
x0 = sp.Symbol("x0", real=True)
x1 = sp.Symbol("x1", real=True)
x  = sp.Matrix([x0, x1])

# create a hybrid model
input_dim = len(x)
model = HybridModel(input_dim)
result = model(x)[0]

The result would be

     2⎛L¹₀   L¹₁⋅x₀   L¹₂⋅x₁⎞    2⎛L²₀   L²₁⋅x₀   L²₂⋅x₁⎞      2⎛L¹₀   L¹₁⋅x₀ 
- sin ⎜─── + ────── + ──────⎟⋅sin ⎜─── + ────── + ──────⎟ - sin ⎜─── + ────── 
      ⎝ 2      2        2   ⎠     ⎝ 2      2        2   ⎠       ⎝ 2      2    

  L¹₂⋅x₁⎞    2⎛L²₀   L²₁⋅x₀   L²₂⋅x₁⎞      2⎛L²₀   L²₁⋅x₀   L²₂⋅x₁⎞    2⎛L¹₀  
+ ──────⎟⋅cos ⎜─── + ────── + ──────⎟ + sin ⎜─── + ────── + ──────⎟⋅cos ⎜─── +
    2   ⎠     ⎝ 2      2        2   ⎠       ⎝ 2      2        2   ⎠     ⎝ 2   

 L¹₁⋅x₀   L¹₂⋅x₁⎞      2⎛L¹₀   L¹₁⋅x₀   L¹₂⋅x₁⎞    2⎛L²₀   L²₁⋅x₀   L²₂⋅x₁⎞
 ────── + ──────⎟ + cos ⎜─── + ────── + ──────⎟⋅cos ⎜─── + ────── + ──────⎟
   2        2   ⎠       ⎝ 2      2        2   ⎠     ⎝ 2      2        2   ⎠

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

qympy-1.5.0.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

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

qympy-1.5.0-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

Details for the file qympy-1.5.0.tar.gz.

File metadata

  • Download URL: qympy-1.5.0.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.12

File hashes

Hashes for qympy-1.5.0.tar.gz
Algorithm Hash digest
SHA256 788476d9acfbfca1be53ac3e8b1a5fc5698c63dbe1233706d70ae3a36959c8c5
MD5 e379c5ed8fc27a1e48338f347cf80c54
BLAKE2b-256 3ffc68e9ba99a8dc2672115dac1fb2f96946ab8c8ca8a7876d965d971d9868d1

See more details on using hashes here.

File details

Details for the file qympy-1.5.0-py3-none-any.whl.

File metadata

  • Download URL: qympy-1.5.0-py3-none-any.whl
  • Upload date:
  • Size: 9.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.12

File hashes

Hashes for qympy-1.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3f1db69e24815b2236bb45861f6ddef49ef2a5a436042daf8e56fea6d3ba25c2
MD5 73fd8d795add94f18bf629e094264f11
BLAKE2b-256 78eef5e75182e47f20e97e0a22374a003f36885154222b6823215703fefdcc49

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