TorchQuantum

repository·main·Indexed 23 days ago

https://github.com/mit-han-lab/torchquantum

A PyTorch-based framework for simulating quantum computations, supporting statevector and pulse simulation on GPUs. It is designed for scalable research in quantum machine learning, algorithm design, and optimal control, featuring capabilities for training Quantum Neural Networks (QNNs), Variational Quantum Eigensolver (VQE) circuits, and implementing Quantum Kernel Methods.

Tokens
14.4K
Snippets
43
Records
60
Agent score
82%

What's inside TorchQuantum

  1. Understand the concept of Quantum Kernel Methods

    main

    Quantum Kernel Methods leverage quantum circuits to map data into a high-dimensional Hilbert space. This space is often computationally difficult for classical computers to simulate, allowing for pattern analysis and non-linear problem solving via linear classifiers (like Support Vector Machines) using the "Kernel Trick."

    To evaluate the distance (inner product) between two data points $x$ and $y$ in this Hilbert space:

    1. Let $S(x)$ be the unitary operator that transfers data $x$ to a state in the Hilbert space.
    2. To find the inner product between $S(x)$ and $S(y)$, apply the transpose conjugation of $S(y)$ after $S(x)$ (i.e., $S(x)S(y)^\dagger$).
    3. Measure the probability that the resulting state falls on the $|00\cdots0\rangle$ state.
  2. Understand the predict_quantum_acc dataset format

    main

    The dataset is stored in the raw_data_qasm folder and is loaded via pickle. Each data entry in the dataset is a list containing three elements:

    1. data[0] (String): The QASM representation of the circuit.
    2. data[1] (Dict): A dictionary containing noise information.
    3. data[2] (Float): The probability of successful trials.
    data_set = pickle.load(file)
    data = data_set[0]
    # data[0]: String, the qasm for the circuit,
    # data[1]: Dict, contains the noise information
    # data[2]: Float, probability of successful trials.
  3. Apply quantum gates using different interfaces

    main

    Torchquantum provides three primary ways to apply gates to a QuantumDevice:

    1. Direct Method (qdev.op): Call gate methods directly on the device instance.
    2. Functional Interface (tqf): Use torchquantum.functional for stateless gate applications.
    3. Operator Classes (tq.Operator): Use parameterized and trainable gate classes (e.g., tq.RX).
    import torchquantum as tq
    import torchquantum.functional as tqf
    
    qdev = tq.QuantumDevice(n_wires=2, bsz=5, device="cpu", record_op=True)
    
    # 1. Direct method
    qdev.h(wires=0)
    qdev.cnot(wires=[0, 1])
    
    # 2. Functional interface
    tqf.h(qdev, wires=1)
    tqf.x(qdev, wires=1)
    
    # 3. Operator classes (supports parameters and training)
    op = tq.RX(has_params=True, trainable=True, init_params=0.5)
    op(qdev, wires=0)
  4. Install torchquantum from source

    main

    To install torchquantum, clone the repository and install it in editable mode using pip.

    git clone https://github.com/mit-han-lab/torchquantum.git
    cd torchquantum
    pip install --editable .
  5. Perform ablation studies for predict_quantum_acc

    main

    You can study the effect of specific features on model performance by passing different configuration flags to train.py.

    Available ablation configurations:

    • huge/layer1: 1 layer of transformer
    • huge/layer3: 3 layers of transformer
    • huge/onlygf: Only use global features
    • huge/wogateerror: No gate error
    • huge/wogateindex: No gate index
    • huge/wogatetype: No gate type
    • huge/wogf: No global features
    • huge/woqubitindex: No qubit index
    • huge/wot1t2: No t1 and t2
    # Default training
    python train.py huge/default
    
    # Ablation studies
    python train.py huge/layer1
    python train.py huge/layer3
    python train.py huge/onlygf
    python train.py huge/wogateerror
    python train.py huge/wogateindex
    python train.py huge/wogatetype
    python train.py huge/wogf
    python train.py huge/woqubitindex
    python train.py huge/wot1t2
  6. Learn TorchQuantum via the ICCAD 2022 Tutorial

    main

    The ICCAD 2022 Tutorial provides a structured learning path for using TorchQuantum, ranging from basic usage to advanced optimizations. The tutorial is divided into three main sections:

    1. TorchQuantum Basic Usage: Covers the fundamental API and core concepts.
    2. Pulse Level Optimizations: Demonstrates how to use TorchQuantum for optimizations at the pulse level.
    3. Gate Level Optimizations: Demonstrates how to use TorchQuantum for optimizations at the gate level.

    You can access the tutorial slides in PDF format or follow the interactive tutorials via Google Colab notebooks provided in each section.

  7. Run the Quantum Kernel Method example in Colab

    main

    You can run the interactive Quantum Kernel Method tutorial directly in Google Colab to experiment with building and training an SVM using quantum kernels. The example demonstrates the use of tq.op_name_dict, tq.functional.func_name_dict, and tq.QuantumDevice from TorchQuantum.

    https://colab.research.google.com/github/mit-han-lab/torchquantum/blob/master/examples/quantum_kernel_method/quantum_kernel_method.ipynb