Package: qympy
Qympy - Quantum Analytic Computation with SympyA sympy based python package for symbolic calculation of quantum circuit and machine learning.
See GitHub:https://github.com/r08222011/QympyInstallationSimply runpip install, seeQympypipinstallqympyGet StartedSeeqympy/example/example_circuit.ipynb1. Circuit InitializationCommon circuits ansatz can be found inqympy.quantum_circuit, mostly follow withQiskit.operations. To build a circuit from beginning, useqympy.quantum_circuit.sp_circuit.Circuit. The basic use ofCircuitis same asQiskit. For example:fromqympy.quantum_circuit.sp_circuitimportCircuitqc=Circuit(3)# initialize a 3-qubit quantum circuitqc.h(0)# Hadamard gate on 0th qubitqc.ry("x",0)# y-rotation on 0th qubit with theta = xqc.rxx("y",1,2)# xx-rotation on 1st and 2nd qubits with theta = yqc.cx(0,1)# CNOT on 1st and 2nd qubitsqc.cz(1,2)# CZ on 1st and 2nd qubits2. Draw the circuitWe now have initialized a quantum circuit. To see the circuit we built, we can useCircuit.draw(). This method useqiskit.circuit.QuantumCircuit.drawwithdraw('mpl')as default. For example:qc.draw("mpl")3. Evolve and measure the circuitThe last step for getting the analytic expression is to call the methodCircuit.evolve(). This will calculate the final state with the gates applied. After evolving the quantum state, we can measure the quantum state withX,Y,Zbasis 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 circuitresult=qc.measure(2,"Z")# measure the 2nd qubit in Z-basisThe result would be2                                    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 LearningSeeqympy/example/example_ml.ipynbIn 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 modelWe construct a hybrid model with aLinearlayer followed by a quantum circuit, which is constructed withAngleEncodingandSingleRot, and finally end up with aMeasurement.importsympyasspfromqympy.quantum_circuit.sp_circuitimportCircuitfromqympy.machine_learning.classicalimportLinearfromqympy.machine_learning.quantumimportMeasurement,AngleEncoding,SingleRotclassHybridModel: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):forsubmodelinself.net:x=submodel(x)returnx2. Feed forward the input dataWe then feed forward a 2-dimensional datax = [x0, x1]# initialize input variables: x0 and x1x0=sp.Symbol("x0",real=True)x1=sp.Symbol("x1",real=True)x=sp.Matrix([x0,x1])# create a hybrid modelinput_dim=len(x)model=HybridModel(input_dim)result=model(x)[0]The result would be2⎛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   ⎠