# Taichi-Q
Quantum Computation Simulator Engine Based on Taichi (available for both GPU and CPU)
**This is a Hackathon Project, Project Introduction available [here](https://bughht.github.io/University_SHU/Taichi-Q%20Introduction)**
## Intallation (only support windows right now)
+ From PyPI
```bash
pip install taichi-q
```
```bash
pip3 install taichi-q
```
## Usage
### Import the Packages
```python
# Required
from taichi_q import Engine, Gate
# Optional
import numpy as np
import taichi as ti
```
### Setup Simulator Engine
**Warning!!** Simulator Engine could only be initialized once.
For CPU
```python
eng=Engine(num_qubits=3,state_init=0,device='cpu')
```
For GPU
```python
eng=Engine(num_qubits=3,state_init=0,device='gpu')
```
### Initialize Qubits
+ All $|0\rangle$ or $|1\rangle$
```python
eng=Engine(num_qubits=3, state_init=0, device='cpu')
eng=Engine(num_qubits=3, state_init=1, device='cpu')
```
+ Choose $|0\rangle$ or $|1\rangle$ of each qubit with list/tuple/ndarray
```python
eng=Engine(num_qubits=3, state_init=[0,1,0], device='gpu')
eng=Engine(num_qubits=3, state_init=(0,1,0), device='gpu')
eng=Engine(num_qubits=3, state_init=np.array([0,1,0]), device='gpu')
```
+ Set complex qubit state with np.array(dtype=complex)
```python
eng = Engine(
num_qubits=3,
state_init=[[-1/np.sqrt(2), j/np.sqrt(2)], [1, 0], [0, 1]],
device='cpu')
```
### Quantum Gate Operators
Quantum Gates could be found in `taichi_q.gates`. Support $H(), X(), Y(), Z(), S(), T(), swap()$
$U(\theta, \phi, \lambda), R_x(\theta), R_y(\theta), R_z(\theta)$
$QFT(n), iQFT(n)$, and all controlled gates.
+ Apply single-qubit gate to target qubit (e.g. H)
```python
eng.Ops(Gate.H(), [0])
```
+ Apply muti-qubit gate to target qubits (e.g. swap)
```python
eng.Ops(Gate.swap(), [0,1])
```
+ Apply controlled-qubit gate to target qubits (e.g. CNOT=CX)
```python
eng.Ops(Gate.X(), [0], [1])
```
+ If you want to print Operated Gate, Tgt and Ctl on the terminal
```python
eng.Ops(Gate.QFT(4), [0,1,2,3], [4], print_output=True)
# Output:
# OPS: QFT Tgt: [0,1,2,3] Ctl [4]
```
### Measure the result of a qubit
**Notice!** Measure is an irreversible process. State of the measured qubit would collapsed into $|0\rangle$ or $|1\rangle$
```python
q0_result=eng.Measure(0)
```
### Check the state of all qubits without measuring any qubit
**Notice!** This is a cheating method from simulator. It's not available for real quantum computer.
Check the state of all qubits is useful for quantum computation algorithm design and debug.
+ Print all qubit states
```python
eng.State_Check(print_state=True)
# Output:
# Q: (0, 0, 0) State:[+0.0000+0.0000j] P:0.0000
# Q: (0, 0, 1) State:[+0.0000+0.0000j] P:0.0000
# Q: (0, 1, 0) State:[+0.0000+0.0000j] P:0.0000
# Q: (0, 1, 1) State:[+0.0000+0.0000j] P:0.0000
# Q: (1, 0, 0) State:[+0.0000+0.0000j] P:0.0000
# Q: (1, 0, 1) State:[+0.0000+0.0000j] P:0.0000
# Q: (1, 1, 0) State:[-0.4472+0.0000j] P:0.2000
# Q: (1, 1, 1) State:[+0.0000+0.8944j] P:0.8000
```
+ Display Box-plot of qubit state probability
```python
eng.State_Check(plot_state=True)
```
![boxplot](img/Boxplot.png)
### Print Quantum Circuit
Quantum Gates and its order are recorded by the engine, and could be displayed on the terminal.
```python
eng.circuit_print()
# Output:
# Q0 →|' ' ' ' '■' 'H' 'M' ' ' ' ' '■'|→ Q0
# Q1 →|'H' '■' 'X' ' ' ' ' 'M' '■' ' '|→ Q1
# Q2 →|' ' 'X' ' ' ' ' ' ' ' ' 'X' 'Z'|→ Q2
```
### Visualize Quantum Circuit (with ti.GUI)
The circuit visualization provides a more elegant approach for circuit visualization, based on ti.GUI.
```python
eng.circuit_visualize()
```
![visualize](img/taichi_q.gif)
## Examples
+ [Generate Bell-State](example/bellstate.py)
+ [Quantum Teleportation](example/teleport.py)
+ [Grover Searching Algorithm](example/grover.py)
## Development
Welcome any contribution!
## License
[Apach-2.0](LICENSE)
Raw data
{
"_id": null,
"home_page": "https://github.com/bughht/Taichi-Q",
"name": "taichi-q",
"maintainer": "",
"docs_url": null,
"requires_python": ">3.6",
"maintainer_email": "",
"keywords": "quantum computation simulator",
"author": "Haotian Hong",
"author_email": "bughht@outlook.com",
"download_url": "https://files.pythonhosted.org/packages/4a/93/9f984514fadb44e9ca12fe61a072d20727886390cc08d333206a62ed3b43/taichi_q-0.0.10.tar.gz",
"platform": null,
"description": "# Taichi-Q\n\nQuantum Computation Simulator Engine Based on Taichi (available for both GPU and CPU)\n\n**This is a Hackathon Project, Project Introduction available [here](https://bughht.github.io/University_SHU/Taichi-Q%20Introduction)**\n\n## Intallation (only support windows right now)\n\n+ From PyPI\n\n```bash\npip install taichi-q\n```\n\n```bash\npip3 install taichi-q\n```\n\n## Usage\n\n### Import the Packages\n\n```python\n# Required\nfrom taichi_q import Engine, Gate\n\n# Optional\nimport numpy as np\nimport taichi as ti\n```\n\n### Setup Simulator Engine\n\n**Warning!!** Simulator Engine could only be initialized once.\n\nFor CPU\n\n```python\neng=Engine(num_qubits=3,state_init=0,device='cpu')\n```\n\nFor GPU\n\n```python\neng=Engine(num_qubits=3,state_init=0,device='gpu')\n```\n\n### Initialize Qubits\n\n+ All $|0\\rangle$ or $|1\\rangle$\n\n```python\neng=Engine(num_qubits=3, state_init=0, device='cpu')\neng=Engine(num_qubits=3, state_init=1, device='cpu')\n```\n\n+ Choose $|0\\rangle$ or $|1\\rangle$ of each qubit with list/tuple/ndarray\n\n```python\neng=Engine(num_qubits=3, state_init=[0,1,0], device='gpu')\neng=Engine(num_qubits=3, state_init=(0,1,0), device='gpu')\neng=Engine(num_qubits=3, state_init=np.array([0,1,0]), device='gpu')\n```\n\n+ Set complex qubit state with np.array(dtype=complex)\n\n```python\neng = Engine(\n num_qubits=3,\n state_init=[[-1/np.sqrt(2), j/np.sqrt(2)], [1, 0], [0, 1]],\n device='cpu')\n```\n\n### Quantum Gate Operators\n\nQuantum Gates could be found in `taichi_q.gates`. Support $H(), X(), Y(), Z(), S(), T(), swap()$\n$U(\\theta, \\phi, \\lambda), R_x(\\theta), R_y(\\theta), R_z(\\theta)$\n$QFT(n), iQFT(n)$, and all controlled gates.\n\n+ Apply single-qubit gate to target qubit (e.g. H)\n\n```python\neng.Ops(Gate.H(), [0])\n```\n\n+ Apply muti-qubit gate to target qubits (e.g. swap)\n\n```python\neng.Ops(Gate.swap(), [0,1])\n```\n\n+ Apply controlled-qubit gate to target qubits (e.g. CNOT=CX)\n\n```python\neng.Ops(Gate.X(), [0], [1])\n```\n\n+ If you want to print Operated Gate, Tgt and Ctl on the terminal\n\n```python\neng.Ops(Gate.QFT(4), [0,1,2,3], [4], print_output=True)\n\n# Output:\n# OPS: QFT Tgt: [0,1,2,3] Ctl [4]\n\n```\n\n### Measure the result of a qubit\n\n**Notice!** Measure is an irreversible process. State of the measured qubit would collapsed into $|0\\rangle$ or $|1\\rangle$\n\n```python\nq0_result=eng.Measure(0)\n```\n\n### Check the state of all qubits without measuring any qubit\n\n**Notice!** This is a cheating method from simulator. It's not available for real quantum computer.\n\nCheck the state of all qubits is useful for quantum computation algorithm design and debug.\n\n+ Print all qubit states\n\n```python\neng.State_Check(print_state=True)\n\n# Output:\n# Q: (0, 0, 0) State:[+0.0000+0.0000j] P:0.0000\n# Q: (0, 0, 1) State:[+0.0000+0.0000j] P:0.0000\n# Q: (0, 1, 0) State:[+0.0000+0.0000j] P:0.0000\n# Q: (0, 1, 1) State:[+0.0000+0.0000j] P:0.0000\n# Q: (1, 0, 0) State:[+0.0000+0.0000j] P:0.0000\n# Q: (1, 0, 1) State:[+0.0000+0.0000j] P:0.0000\n# Q: (1, 1, 0) State:[-0.4472+0.0000j] P:0.2000\n# Q: (1, 1, 1) State:[+0.0000+0.8944j] P:0.8000\n```\n\n+ Display Box-plot of qubit state probability\n\n```python\neng.State_Check(plot_state=True)\n```\n\n![boxplot](img/Boxplot.png)\n\n### Print Quantum Circuit\n\nQuantum Gates and its order are recorded by the engine, and could be displayed on the terminal.\n\n```python\neng.circuit_print()\n\n# Output:\n# Q0 \u2192|' ' ' ' '\u25a0' 'H' 'M' ' ' ' ' '\u25a0'|\u2192 Q0\n# Q1 \u2192|'H' '\u25a0' 'X' ' ' ' ' 'M' '\u25a0' ' '|\u2192 Q1\n# Q2 \u2192|' ' 'X' ' ' ' ' ' ' ' ' 'X' 'Z'|\u2192 Q2\n```\n\n### Visualize Quantum Circuit (with ti.GUI)\n\nThe circuit visualization provides a more elegant approach for circuit visualization, based on ti.GUI.\n\n```python\neng.circuit_visualize()\n```\n\n![visualize](img/taichi_q.gif)\n\n## Examples\n\n+ [Generate Bell-State](example/bellstate.py)\n+ [Quantum Teleportation](example/teleport.py)\n+ [Grover Searching Algorithm](example/grover.py)\n\n## Development\n\nWelcome any contribution!\n\n## License\n\n[Apach-2.0](LICENSE)\n\n",
"bugtrack_url": null,
"license": "Apache License Version 2.0",
"summary": "Taichi-Q: A quantum circuit simulator for both CPU and GPU",
"version": "0.0.10",
"split_keywords": [
"quantum",
"computation",
"simulator"
],
"urls": [
{
"comment_text": "",
"digests": {
"md5": "9f11bc1141df631de4812f7088ada3b3",
"sha256": "2595e2e314cb155b61c949df768f46abfd60a3191e836d23afcdc1c367075ec7"
},
"downloads": -1,
"filename": "taichi_q-0.0.10-py3-none-any.whl",
"has_sig": false,
"md5_digest": "9f11bc1141df631de4812f7088ada3b3",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">3.6",
"size": 16500,
"upload_time": "2022-12-03T23:27:40",
"upload_time_iso_8601": "2022-12-03T23:27:40.903887Z",
"url": "https://files.pythonhosted.org/packages/8b/c1/18a1211a06aead12234fe60f574e27de58c939edb1366446d206ed15aeba/taichi_q-0.0.10-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"md5": "87b949ae2c76033fbde55ca20448a03b",
"sha256": "88c8e76edbca792ba2d59875b14436495c5b0b858b68ec536b204e242ef33157"
},
"downloads": -1,
"filename": "taichi_q-0.0.10.tar.gz",
"has_sig": false,
"md5_digest": "87b949ae2c76033fbde55ca20448a03b",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">3.6",
"size": 15137,
"upload_time": "2022-12-03T23:27:43",
"upload_time_iso_8601": "2022-12-03T23:27:43.194056Z",
"url": "https://files.pythonhosted.org/packages/4a/93/9f984514fadb44e9ca12fe61a072d20727886390cc08d333206a62ed3b43/taichi_q-0.0.10.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2022-12-03 23:27:43",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "bughht",
"github_project": "Taichi-Q",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "taichi-q"
}