Skip to content

tencent-quantum-lab/tensorcircuit

Repository files navigation

English | ็ฎ€ไฝ“ไธญๆ–‡

TensorCircuit is the next generation of quantum software framework with support for automatic differentiation, just-in-time compiling, hardware acceleration, and vectorized parallelism.

TensorCircuit is built on top of modern machine learning frameworks: Jax, TensorFlow, and PyTorch. It is specifically suitable for highly efficient simulations of quantum-classical hybrid paradigm and variational quantum algorithms in ideal, noisy and approximate cases. It also supports real quantum hardware access and provides CPU/GPU/QPU hybrid deployment solutions since v0.9.

Getting Started

Please begin with Quick Start in the full documentation.

For more information on software usage, sota algorithm implementation and engineer paradigm demonstration, please refer to 70+ example scripts and 30+ tutorial notebooks. API docstrings and test cases in tests are also informative.

The following are some minimal demos.

  • Circuit manipulation:
import tensorcircuit as tc
c = tc.Circuit(2)
c.H(0)
c.CNOT(0,1)
c.rx(1, theta=0.2)
print(c.wavefunction())
print(c.expectation_ps(z=[0, 1]))
print(c.sample(allow_state=True, batch=1024, format="count_dict_bin"))
  • Runtime behavior customization:
tc.set_backend("tensorflow")
tc.set_dtype("complex128")
tc.set_contractor("greedy")
  • Automatic differentiations with jit:
def forward(theta):
    c = tc.Circuit(2)
    c.R(0, theta=theta, alpha=0.5, phi=0.8)
    return tc.backend.real(c.expectation((tc.gates.z(), [0])))

g = tc.backend.grad(forward)
g = tc.backend.jit(g)
theta = tc.array_to_tensor(1.0)
print(g(theta))
More highlight features for TensorCircuit (click for details)
  • Sparse Hamiltonian generation and expectation evaluation:
n = 6
pauli_structures = []
weights = []
for i in range(n):
    pauli_structures.append(tc.quantum.xyz2ps({"z": [i, (i + 1) % n]}, n=n))
    weights.append(1.0)
for i in range(n):
    pauli_structures.append(tc.quantum.xyz2ps({"x": [i]}, n=n))
    weights.append(-1.0)
h = tc.quantum.PauliStringSum2COO(pauli_structures, weights)
print(h)
# BCOO(complex64[64, 64], nse=448)
c = tc.Circuit(n)
c.h(range(n))
energy = tc.templates.measurements.operator_expectation(c, h)
# -6
  • Large-scale simulation with tensor network engine
# tc.set_contractor("cotengra-30-10")
n=500
c = tc.Circuit(n)
c.h(0)
c.cx(range(n-1), range(1, n))
c.expectation_ps(z=[0, n-1], reuse=False)
  • Density matrix simulator and quantum info quantities
c = tc.DMCircuit(2)
c.h(0)
c.cx(0, 1)
c.depolarizing(1, px=0.1, py=0.1, pz=0.1)
dm = c.state()
print(tc.quantum.entropy(dm))
print(tc.quantum.entanglement_entropy(dm, [0]))
print(tc.quantum.entanglement_negativity(dm, [0]))
print(tc.quantum.log_negativity(dm, [0]))

Install

The package is written in pure Python and can be obtained via pip as:

pip install tensorcircuit

We recommend you install this package with tensorflow also installed as:

pip install tensorcircuit[tensorflow]

Other optional dependencies include [torch], [jax], [qiskit] and [cloud].

We also have Docker support.

Advantages

  • Tensor network simulation engine based

  • JIT, AD, vectorized parallelism compatible

  • GPU support, quantum device access support, hybrid deployment support

  • Efficiency

    • Time: 10 to 10^6+ times acceleration compared to TensorFlow Quantum, Pennylane or Qiskit

    • Space: 600+ qubits 1D VQE workflow (converged energy inaccuracy: < 1%)

  • Elegance

    • Flexibility: customized contraction, multiple ML backend/interface choices, multiple dtype precisions, multiple QPU providers

    • API design: quantum for humans, less code, more power

  • Batteries included

    Tons of amazing features and built in tools for research (click for details)
    • Support super large circuit simulation using tensor network engine.

    • Support noisy simulation with both Monte Carlo and density matrix (tensor network powered) modes.

    • Support approximate simulation with MPS-TEBD modes.

    • Support analog/digital hybrid simulation (time dependent Hamiltonian evolution, pulse level simulation) with neural ode modes.

    • Support Fermion Gaussian state simulation with expectation, entanglement, measurement, ground state, real and imaginary time evolution.

    • Support qudits simulation.

    • Support parallel quantum circuit evaluation across multiple GPUs.

    • Highly customizable noise model with gate error and scalable readout error.

    • Support for non-unitary gate and post-selection simulation.

    • Support real quantum devices access from different providers.

    • Scalable readout error mitigation native to both bitstring and expectation level with automatic qubit mapping consideration.

    • Advanced quantum error mitigation methods and pipelines such as ZNE, DD, RC, etc.

    • Support MPS/MPO as representations for input states, quantum gates and observables to be measured.

    • Support vectorized parallelism on circuit inputs, circuit parameters, circuit structures, circuit measurements and these vectorization can be nested.

    • Gradients can be obtained with both automatic differenation and parameter shift (vmap accelerated) modes.

    • Machine learning interface/layer/model abstraction in both TensorFlow and PyTorch for both numerical simulation and real QPU experiments.

    • Circuit sampling supports both final state sampling and perfect sampling from tensor networks.

    • Light cone reduction support for local expectation calculation.

    • Highly customizable tensor network contraction path finder with opteinsum interface.

    • Observables are supported in measurement, sparse matrix, dense matrix and MPO format.

    • Super fast weighted sum Pauli string Hamiltonian matrix generation.

    • Reusable common circuit/measurement/problem templates and patterns.

    • Jittable classical shadow infrastructures.

    • SOTA quantum algorithm and model implementations.

    • Support hybrid workflows and pipelines with CPU/GPU/QPU hardware from local/cloud/hpc resources using tf/torch/jax/cupy/numpy frameworks all at the same time.

Contributing

Status

This project is created and maintained by Shi-Xin Zhang with current core authors Shi-Xin Zhang and Yu-Qin Chen. We also thank contributions from the open source community.

Citation

If this project helps in your research, please cite our software whitepaper to acknowledge the work put into the development of TensorCircuit.

TensorCircuit: a Quantum Software Framework for the NISQ Era (published in Quantum)

which is also a good introduction to the software.

Research works citing TensorCircuit can be highlighted in Research and Applications section.

Guidelines

For contribution guidelines and notes, see CONTRIBUTING.

We welcome issues, PRs, and discussions from everyone, and these are all hosted on GitHub.

License

TensorCircuit is open source, released under the Apache License, Version 2.0.

Contributors

Shixin Zhang
Shixin Zhang

๐Ÿ’ป ๐Ÿ“– ๐Ÿ’ก ๐Ÿค” ๐Ÿš‡ ๐Ÿšง ๐Ÿ”ฌ ๐Ÿ‘€ ๐ŸŒ โš ๏ธ โœ… ๐Ÿ“ข ๐Ÿ’ฌ
Yuqin Chen
Yuqin Chen

๐Ÿ’ป ๐Ÿ“– ๐Ÿ’ก ๐Ÿค” ๐Ÿ”ฌ โš ๏ธ โœ… ๐Ÿ“ข
Jiezhong Qiu
Jiezhong Qiu

๐Ÿ’ป ๐Ÿ’ก ๐Ÿค” ๐Ÿ”ฌ
Weitang Li
Weitang Li

๐Ÿ’ป ๐Ÿ“– ๐Ÿค” ๐Ÿ”ฌ โš ๏ธ ๐Ÿ“ข
Jiace Sun
Jiace Sun

๐Ÿ’ป ๐Ÿ“– ๐Ÿ’ก ๐Ÿค” ๐Ÿ”ฌ โš ๏ธ
Zhouquan Wan
Zhouquan Wan

๐Ÿ’ป ๐Ÿ“– ๐Ÿ’ก ๐Ÿค” ๐Ÿ”ฌ โš ๏ธ โœ…
Shuo Liu
Shuo Liu

๐Ÿ’ก ๐Ÿ”ฌ โœ…
Hao Yu
Hao Yu

๐Ÿ’ป ๐Ÿ“– ๐Ÿš‡ โš ๏ธ โœ…
Xinghan Yang
Xinghan Yang

๐Ÿ“– ๐ŸŒ โœ…
JachyMeow
JachyMeow

โœ… ๐ŸŒ
Zhaofeng Ye
Zhaofeng Ye

๐ŸŽจ
erertertet
erertertet

๐Ÿ’ป ๐Ÿ“– โš ๏ธ
Yicong Zheng
Yicong Zheng

โœ…
Zixuan Song
Zixuan Song

๐Ÿ“– ๐ŸŒ ๐Ÿ’ป โš ๏ธ
Hao Xie
Hao Xie

๐Ÿ“–
Pramit Singh
Pramit Singh

โš ๏ธ
Jonathan Allcock
Jonathan Allcock

๐Ÿ“– ๐Ÿค” ๐Ÿ“ข
nealchen2003
nealchen2003

๐Ÿ“–
้šๅ…ฌ่ง‚้ฑผ
้šๅ…ฌ่ง‚้ฑผ

๐Ÿ’ป โš ๏ธ
WiuYuan
WiuYuan

๐Ÿ’ก
Felix Xu
Felix Xu

โœ… ๐Ÿ’ป โš ๏ธ
Hong-Ye Hu
Hong-Ye Hu

๐Ÿ“–
peilin
peilin

โœ… ๐Ÿ’ป โš ๏ธ ๐Ÿ“–
Cristian Emiliano Godinez Ramirez
Cristian Emiliano Godinez Ramirez

๐Ÿ’ป โš ๏ธ
ztzhu
ztzhu

๐Ÿ’ป
Rabqubit
Rabqubit

๐Ÿ’ก
Kazuki Tsuoka
Kazuki Tsuoka

๐Ÿ’ป โš ๏ธ ๐Ÿ“– ๐Ÿ’ก
Gopal Ramesh Dahale
Gopal Ramesh Dahale

๐Ÿ’ก
Chanandellar Bong
Chanandellar Bong

๐Ÿ’ก

Research and Applications

DQAS

For the application of Differentiable Quantum Architecture Search, see applications.

Reference paper: https://arxiv.org/abs/2010.08561 (published in QST).

VQNHE

For the application of Variational Quantum-Neural Hybrid Eigensolver, see applications.

Reference paper: https://arxiv.org/abs/2106.05105 (published in PRL) and https://arxiv.org/abs/2112.10380 (published in AQT).

VQEX-MBL

For the application of VQEX on MBL phase identification, see the tutorial.

Reference paper: https://arxiv.org/abs/2111.13719 (published in PRB).

Stark-DTC

For the numerical demosntration of discrete time crystal enabled by Stark many-body localization, see the Floquet simulation demo.

Reference paper: https://arxiv.org/abs/2208.02866 (published in PRL).

RA-Training

For the numerical simulation of variational quantum algorithm training using random gate activation strategy by us, see the project repo.

Reference paper: https://arxiv.org/abs/2303.08154 (published in PRR as a Letter).

TenCirChem

TenCirChem is an efficient and versatile quantum computation package for molecular properties. TenCirChem is based on TensorCircuit and is optimized for chemistry applications.

Reference paper: https://arxiv.org/abs/2303.10825 (published in JCTC).

EMQAOA-DARBO

For the numerical simulation and hardware experiments with error mitigation on QAOA, see the project repo.

Reference paper: https://arxiv.org/abs/2303.14877 (published in Communications Physics).

NN-VQA

For the setup and simulation code of neural network encoded variational quantum eigensolver, see the demo.

Reference paper: https://arxiv.org/abs/2308.01068 (published in PRApplied).

More works

More research works and code projects using TensorCircuit (click for details)

If you want to highlight your research work or projects here, feel free to add by opening PR.