Git Product home page Git Product logo

qympy's Introduction

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   ⎠

qympy's People

Contributors

maplexgitx0302 avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.