qympy


Nameqympy JSON
Version 1.5.0 PyPI version JSON
download
home_page
SummarySymbolic Calculation of Quantum Computation with Sympy
upload_time2023-01-13 10:17:12
maintainer
docs_urlNone
author
requires_python
licenseMIT License Copyright (c) 2022 Yi-An Chen Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords sympy qiskit quantum
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            ![plot](./Logo/Qympy_Long_Logo.png)

# 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](https://pypi.org/project/qympy/)

```bash
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](https://qiskit.org/documentation/tutorials/circuits/3_summary_of_quantum_operations.html). To build a circuit from beginning, use `qympy.quantum_circuit.sp_circuit.Circuit`. The basic use of `Circuit` is same as [Qiskit](https://qiskit.org). For example:
```python
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](https://qiskit.org/documentation/stubs/qiskit.circuit.QuantumCircuit.draw.html) with `draw('mpl')` as default. For example:

```python
qc.draw("mpl")
```
![plot](./src/qympy/example/example_circuit.png)

**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:
```python
'''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

```txt
                           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`.

```python
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]`

```python
# 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

```txt
     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   ⎠
```

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "qympy",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "Sympy,Qiskit,Quantum",
    "author": "",
    "author_email": "Yi-An Chen <r08222011@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/3f/fc/68e9ba99a8dc2672115dac1fb2f96946ab8c8ca8a7876d965d971d9868d1/qympy-1.5.0.tar.gz",
    "platform": null,
    "description": "![plot](./Logo/Qympy_Long_Logo.png)\n\n# Qympy - Quantum Analytic Computation with Sympy\nA sympy based python package for symbolic calculation of quantum circuit and machine learning.\nSee GitHub: https://github.com/r08222011/Qympy\n\n---\n\n### Installation\nSimply run `pip install`, see [Qympy](https://pypi.org/project/qympy/)\n\n```bash\npip install qympy\n```\n\n### Get Started\nSee `qympy/example/example_circuit.ipynb`\n\n**1. Circuit Initialization**\nCommon circuits ansatz can be found in `qympy.quantum_circuit`, mostly follow with [Qiskit.operations](https://qiskit.org/documentation/tutorials/circuits/3_summary_of_quantum_operations.html). To build a circuit from beginning, use `qympy.quantum_circuit.sp_circuit.Circuit`. The basic use of `Circuit` is same as [Qiskit](https://qiskit.org). For example:\n```python\nfrom qympy.quantum_circuit.sp_circuit import Circuit\n\nqc = Circuit(3)   # initialize a 3-qubit quantum circuit\nqc.h(0)           # Hadamard gate on 0th qubit\nqc.ry(\"x\", 0)     # y-rotation on 0th qubit with theta = x\nqc.rxx(\"y\", 1, 2) # xx-rotation on 1st and 2nd qubits with theta = y\nqc.cx(0,1)        # CNOT on 1st and 2nd qubits\nqc.cz(1,2)        # CZ on 1st and 2nd qubits\n```\n\n**2. Draw the circuit**\nWe now have initialized a quantum circuit. To see the circuit we built, we can use `Circuit.draw()`. This method use [qiskit.circuit.QuantumCircuit.draw](https://qiskit.org/documentation/stubs/qiskit.circuit.QuantumCircuit.draw.html) with `draw('mpl')` as default. For example:\n\n```python\nqc.draw(\"mpl\")\n```\n![plot](./src/qympy/example/example_circuit.png)\n\n**3. Evolve and measure the circuit**\nThe 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:\n```python\n'''It would be a good habit to evolve the state first.\nAlthough when using 'measure' qympy will automatically evolve if you haven't evolve.\nWe design in this way since we won't always need to know the final state for every case.'''\n\nqc.evolve() # evolve the circuit\nresult = qc.measure(2, \"Z\") # measure the 2nd qubit in Z-basis\n```\n\nThe result would be\n\n```txt\n                           2                                    2             \n  \u239b        \u239bx\u239e         \u239bx\u239e\u239e            \u239b        \u239bx\u239e         \u239bx\u239e\u239e            \u239b \n  \u239c  \u221a2\u22c5sin\u239c\u2500\u239f   \u221a2\u22c5cos\u239c\u2500\u239f\u239f            \u239c  \u221a2\u22c5sin\u239c\u2500\u239f   \u221a2\u22c5cos\u239c\u2500\u239f\u239f            \u239c\u221a\n  \u239c        \u239d2\u23a0         \u239d2\u23a0\u239f     2\u239by\u239e   \u239c        \u239d2\u23a0         \u239d2\u23a0\u239f     2\u239by\u239e   \u239c \n- \u239c- \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 + \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u239f \u22c5sin \u239c\u2500\u239f + \u239c- \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 + \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u239f \u22c5cos \u239c\u2500\u239f - \u239c\u2500\n  \u239d      2           2    \u23a0      \u239d2\u23a0   \u239d      2           2    \u23a0      \u239d2\u23a0   \u239d \n\n                     2                                  2        \n     \u239bx\u239e         \u239bx\u239e\u239e            \u239b      \u239bx\u239e         \u239bx\u239e\u239e         \n2\u22c5sin\u239c\u2500\u239f   \u221a2\u22c5cos\u239c\u2500\u239f\u239f            \u239c\u221a2\u22c5sin\u239c\u2500\u239f   \u221a2\u22c5cos\u239c\u2500\u239f\u239f         \n     \u239d2\u23a0         \u239d2\u23a0\u239f     2\u239by\u239e   \u239c      \u239d2\u23a0         \u239d2\u23a0\u239f     2\u239by\u239e\n\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 + \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u239f \u22c5sin \u239c\u2500\u239f + \u239c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 + \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u239f \u22c5cos \u239c\u2500\u239f\n   2           2    \u23a0      \u239d2\u23a0   \u239d    2           2    \u23a0      \u239d2\u23a0\n```\n\n### Quantum Machine Learning\nSee `qympy/example/example_ml.ipynb`\n\nIn 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.\n\n**1. Contruct a hybrid model**\nWe 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`.\n\n```python\nimport sympy as sp\nfrom qympy.quantum_circuit.sp_circuit import Circuit\nfrom qympy.machine_learning.classical import Linear\nfrom qympy.machine_learning.quantum import Measurement, AngleEncoding, SingleRot\n\nclass HybridModel:\n    def __init__(self, input_dim):\n        self.net = [\n            Linear(input_dim, input_dim),\n            AngleEncoding(input_dim, rot_gate=\"ry\") + SingleRot(input_dim, rot_mode=['rz'], ent_mode='cx'),\n            Measurement(qubits=[0], bases=[\"Z\"]),\n        ]\n    def __call__(self, x):\n        for submodel in self.net:\n            x = submodel(x)\n        return x\n```\n\n**2. Feed forward the input data**\nWe then feed forward a 2-dimensional data `x = [x0, x1]`\n\n```python\n# initialize input variables: x0 and x1\nx0 = sp.Symbol(\"x0\", real=True)\nx1 = sp.Symbol(\"x1\", real=True)\nx  = sp.Matrix([x0, x1])\n\n# create a hybrid model\ninput_dim = len(x)\nmodel = HybridModel(input_dim)\nresult = model(x)[0]\n```\n\nThe result would be\n\n```txt\n     2\u239bL\u00b9\u2080   L\u00b9\u2081\u22c5x\u2080   L\u00b9\u2082\u22c5x\u2081\u239e    2\u239bL\u00b2\u2080   L\u00b2\u2081\u22c5x\u2080   L\u00b2\u2082\u22c5x\u2081\u239e      2\u239bL\u00b9\u2080   L\u00b9\u2081\u22c5x\u2080 \n- sin \u239c\u2500\u2500\u2500 + \u2500\u2500\u2500\u2500\u2500\u2500 + \u2500\u2500\u2500\u2500\u2500\u2500\u239f\u22c5sin \u239c\u2500\u2500\u2500 + \u2500\u2500\u2500\u2500\u2500\u2500 + \u2500\u2500\u2500\u2500\u2500\u2500\u239f - sin \u239c\u2500\u2500\u2500 + \u2500\u2500\u2500\u2500\u2500\u2500 \n      \u239d 2      2        2   \u23a0     \u239d 2      2        2   \u23a0       \u239d 2      2    \n\n  L\u00b9\u2082\u22c5x\u2081\u239e    2\u239bL\u00b2\u2080   L\u00b2\u2081\u22c5x\u2080   L\u00b2\u2082\u22c5x\u2081\u239e      2\u239bL\u00b2\u2080   L\u00b2\u2081\u22c5x\u2080   L\u00b2\u2082\u22c5x\u2081\u239e    2\u239bL\u00b9\u2080  \n+ \u2500\u2500\u2500\u2500\u2500\u2500\u239f\u22c5cos \u239c\u2500\u2500\u2500 + \u2500\u2500\u2500\u2500\u2500\u2500 + \u2500\u2500\u2500\u2500\u2500\u2500\u239f + sin \u239c\u2500\u2500\u2500 + \u2500\u2500\u2500\u2500\u2500\u2500 + \u2500\u2500\u2500\u2500\u2500\u2500\u239f\u22c5cos \u239c\u2500\u2500\u2500 +\n    2   \u23a0     \u239d 2      2        2   \u23a0       \u239d 2      2        2   \u23a0     \u239d 2   \n\n L\u00b9\u2081\u22c5x\u2080   L\u00b9\u2082\u22c5x\u2081\u239e      2\u239bL\u00b9\u2080   L\u00b9\u2081\u22c5x\u2080   L\u00b9\u2082\u22c5x\u2081\u239e    2\u239bL\u00b2\u2080   L\u00b2\u2081\u22c5x\u2080   L\u00b2\u2082\u22c5x\u2081\u239e\n \u2500\u2500\u2500\u2500\u2500\u2500 + \u2500\u2500\u2500\u2500\u2500\u2500\u239f + cos \u239c\u2500\u2500\u2500 + \u2500\u2500\u2500\u2500\u2500\u2500 + \u2500\u2500\u2500\u2500\u2500\u2500\u239f\u22c5cos \u239c\u2500\u2500\u2500 + \u2500\u2500\u2500\u2500\u2500\u2500 + \u2500\u2500\u2500\u2500\u2500\u2500\u239f\n   2        2   \u23a0       \u239d 2      2        2   \u23a0     \u239d 2      2        2   \u23a0\n```\n",
    "bugtrack_url": null,
    "license": "MIT License  Copyright (c) 2022 Yi-An Chen  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.",
    "summary": "Symbolic Calculation of Quantum Computation with Sympy",
    "version": "1.5.0",
    "split_keywords": [
        "sympy",
        "qiskit",
        "quantum"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "78eef5e75182e47f20e97e0a22374a003f36885154222b6823215703fefdcc49",
                "md5": "73fd8d795add94f18bf629e094264f11",
                "sha256": "3f1db69e24815b2236bb45861f6ddef49ef2a5a436042daf8e56fea6d3ba25c2"
            },
            "downloads": -1,
            "filename": "qympy-1.5.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "73fd8d795add94f18bf629e094264f11",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 9292,
            "upload_time": "2023-01-13T10:17:09",
            "upload_time_iso_8601": "2023-01-13T10:17:09.967489Z",
            "url": "https://files.pythonhosted.org/packages/78/ee/f5e75182e47f20e97e0a22374a003f36885154222b6823215703fefdcc49/qympy-1.5.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "3ffc68e9ba99a8dc2672115dac1fb2f96946ab8c8ca8a7876d965d971d9868d1",
                "md5": "e379c5ed8fc27a1e48338f347cf80c54",
                "sha256": "788476d9acfbfca1be53ac3e8b1a5fc5698c63dbe1233706d70ae3a36959c8c5"
            },
            "downloads": -1,
            "filename": "qympy-1.5.0.tar.gz",
            "has_sig": false,
            "md5_digest": "e379c5ed8fc27a1e48338f347cf80c54",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 10049,
            "upload_time": "2023-01-13T10:17:12",
            "upload_time_iso_8601": "2023-01-13T10:17:12.241403Z",
            "url": "https://files.pythonhosted.org/packages/3f/fc/68e9ba99a8dc2672115dac1fb2f96946ab8c8ca8a7876d965d971d9868d1/qympy-1.5.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-01-13 10:17:12",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "lcname": "qympy"
}
        
Elapsed time: 0.02871s