You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: llm.md
+62-60Lines changed: 62 additions & 60 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,26 +1,33 @@
1
1
# TensorCircuit-NG Repository Guide for AI Agents
2
2
3
-
## Repository Overview
3
+
## Core Philosophy
4
4
5
-
TensorCircuit is a high-performance unified quantum computing framework designed for the NISQ (Noisy Intermediate-Scale Quantum) era. It provides a comprehensive set of tools for quantum circuit simulation with support for multiple backends including Numpy, TensorFlow, JAX, and PyTorch.
5
+
TensorCircuit is a **Tensor Network-first**, **Multi-Backend** quantum computing framework. When contributing to this codebase, you must adhere to the following architectural principles:
6
6
7
-
## Documentation and AI Native Services
7
+
### Unified Backend Interface
8
+
-**Rule**: Never use backend-specific libraries (numpy, tensorflow, jax) directly in core logic.
9
+
-**Pattern**: Use the `tc.backend` abstraction for all tensor operations.
10
+
-**Example**: Use `tc.backend.sin(x)` instead of `np.sin(x)` or `tf.math.sin(x)`. This ensures code runs seamlessly on TensorFlow, JAX, PyTorch, and NumPy.
8
11
9
-
### Official Documentation
12
+
### Differentiable Programming (AD)
13
+
-**Rule**: All core components must be differentiable.
14
+
-**Pattern**: Avoid operations that break the computation graph (e.g., converting to numpy inside a differentiable function, using in-place assignments on tensors).
15
+
-**Goal**: End-to-end differentiability allows variational algorithms and gradient-based optimization to work out of the box.
10
16
11
-
- Main Documentation: https://tensorcircuit-ng.readthedocs.io/
-API Reference: Available in docstrings throughout the codebase
17
+
### JIT-First Optimization
18
+
-**Rule**: Write code that is JIT-compilable (Just-In-Time).
19
+
-**Pattern**: Avoid Python control flow (if/else) that depends on tensor values. Use `tc.backend.cond` or `tc.backend.switch` if necessary, or structure code to be statically analyzable.
20
+
-**Benefit**: This enables massive speedups on JAX and TensorFlow backends.
# TensorCircuit Development Experience & Protocols
2
+
3
+
This document records specific technical protocols, lessons learned, and advanced practices for developing TensorCircuit. Refer to this when implementing complex features or debugging performance issues.
4
+
5
+
## JAX, Compilation, and Performance
6
+
7
+
1.**Benchmarking JAX Code**:
8
+
* JAX execution is asynchronous. To measure execution time accurately, always call `.block_until_ready()` on the result's data buffer (e.g., `result.data.block_until_ready()`).
9
+
* Failing to block will only measure the dispatch time, leading to confusing results (e.g., "first run faster than second run").
10
+
11
+
2.**Memory Management (OOM Prevention)**:
12
+
* For operations over large batches (e.g., summing $2^{22}$ Pauli strings), `vmap` materializes all intermediate results in memory.
13
+
***Protocol**: Use `jax.lax.scan` or sequential loops for reductions over large inputs to keep peak memory usage constant ($O(1)$) rather than linear ($O(N)$).
***Protocol**: When handling sparse outputs, do NOT assume specific attributes like `.row` or `.col` exist (missing in JAX BCOO or Scipy CSR).
20
+
***Check**: Use `hasattr(obj, "tocoo")` to detect Scipy sparse matrices and convert them if a standard COO interface is needed.
21
+
***Comparison**: Compare sparse matrices by subtraction (`abs(A - B).max() ≈ 0`) rather than element-wise attribute checks, to be robust against format differences.
22
+
23
+
## Visualization
24
+
25
+
1.**Non-Blocking Plots**:
26
+
* Library plotting functions (e.g., `Lattice.show`) must accept an optional `ax` (Matplotlib Axis) argument.
27
+
***Protocol**: Draw on the provided `ax` and avoid calling `plt.show()` inside library functions. This allows users to embed plots in subplots and manage the figure lifecycle (saving/closing) themselves.
28
+
29
+
## Benchmarking External Libraries
30
+
31
+
1.**Environment Isolation**:
32
+
* When benchmarking against external libraries (e.g., QuSpin, Quimb, Qiskit) that may have conflicting dependencies, create a dedicated Conda environment (e.g., `bench_quspin`).
33
+
* Install the external library there using pip from the specific conda environment and run the benchmark script using that specific interpreter.
34
+
35
+
2.**Fair Comparison**:
36
+
* Ensure "apples-to-apples" settings (e.g., same precision `complex128`) to support valid performance claims.
0 commit comments