Skip to content

Commit e6c282c

Browse files
update doc
1 parent 7d47e45 commit e6c282c

2 files changed

Lines changed: 114 additions & 0 deletions

File tree

docs/source/advance.rst

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,101 @@ Example: Quantum Teleportation
741741
742742
743743
744+
Symbolic Circuit
745+
-----------------
746+
747+
TensorCircuit-NG supports symbolic circuit simulation via ``tc.SymbolCircuit`` and SymPy.
748+
This allows developers to build circuits with symbolic parameters, derive closed-form algebraic expressions for quantum states, expectation values, or amplitudes, and then lambdify or compile them for fast execution on various backends.
749+
750+
**Basic Usage**
751+
752+
To build a symbolic circuit, we can instantiate ``tc.SymbolCircuit`` and define symbols using SymPy:
753+
754+
.. code-block:: python
755+
756+
import sympy
757+
import tensorcircuit as tc
758+
759+
# Define symbolic parameters
760+
theta = sympy.Symbol("theta", real=True)
761+
phi = sympy.Symbol("phi", real=True)
762+
763+
# Create a symbolic circuit
764+
sc = tc.SymbolCircuit(2)
765+
sc.h(0)
766+
sc.rx(0, theta=theta)
767+
sc.ry(1, theta=phi)
768+
sc.cnot(0, 1)
769+
770+
# Retrieve free symbols
771+
print(sc.free_symbols()) # {theta, phi}
772+
773+
**Evaluating Symbolic Expectation Values**
774+
775+
We can evaluate symbolic expectation values, which returns algebraic SymPy expressions:
776+
777+
.. code-block:: python
778+
779+
# Obtain symbolic expectation value of Z_0
780+
exp_z0 = sc.expectation_ps(z=[0])
781+
print(exp_z0) # Algebraic expression in terms of theta
782+
783+
**Converting to a Concrete Circuit**
784+
785+
To evaluate the circuit at specific parameter values, we can map the symbols to concrete numerical values and convert it to a regular ``tc.Circuit``:
786+
787+
.. code-block:: python
788+
789+
# Bind concrete values to symbols
790+
c = sc.to_circuit({theta: 0.5, phi: 0.1})
791+
792+
# Evaluate the concrete state or expectation values
793+
print(c.state())
794+
print(c.expectation_ps(z=[0]))
795+
796+
**Qiskit Parameterized Circuit Translation**
797+
798+
We can translate a ``SymbolCircuit`` into a Qiskit ``QuantumCircuit``, which preserves Qiskit ``Parameter`` objects for further analysis or hardware deployment:
799+
800+
.. code-block:: python
801+
802+
# Convert symbolic circuit to Qiskit Circuit (keeps Qiskit Parameter objects)
803+
qc = sc.to_qiskit()
804+
print(qc.parameters) # ParameterView([Parameter(phi), Parameter(theta)])
805+
806+
**JAX Lambdify Optimization**
807+
808+
We can convert symbolic expressions to highly optimized backend-native functions (like JAX) using ``sympy.lambdify``, which can then be composed with ``jit``, ``grad``, or ``vmap``:
809+
810+
.. code-block:: python
811+
812+
import sympy
813+
import jax.numpy as jnp
814+
import tensorcircuit as tc
815+
816+
tc.set_backend("jax")
817+
818+
theta = sympy.Symbol("theta", real=True)
819+
sc = tc.SymbolCircuit(1)
820+
sc.rx(0, theta=theta)
821+
exp = sc.expectation_ps(z=[0])
822+
823+
# Convert SymPy expression to JAX-compatible function
824+
# Note: Use modules=[jnp, "math"] instead of string "jax.numpy"
825+
f_jax = sympy.lambdify([theta], exp, modules=[jnp, "math"])
826+
827+
# Compiling with JIT and computing gradients
828+
f_jit = tc.backend.jit(f_jax)
829+
f_grad = tc.backend.jit(tc.backend.grad(f_jax))
830+
831+
print(f_jit(0.5))
832+
print(f_grad(0.5))
833+
834+
For an end-to-end example implementing symbolic QAOA, please refer to the script `qaoa_symbolic.py <https://github.com/tensorcircuit/tensorcircuit-ng/blob/master/examples/qaoa_symbolic.py>`_.
835+
836+
837+
838+
744839
Fermion Gaussian State Simulator
745840
--------------------------------
746841

docs/source/agentic.rst

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,25 @@ Recommended Workflow
6969

7070
By integrating extreme performance with an autonomous, intent-driven AI workflow, TensorCircuit-NG empowers researchers to transition from manual coding to automated scientific discovery.
7171

72+
Initial Prompt for Empty Workspace Setup
73+
----------------------------------------
74+
75+
When starting in a completely empty folder/workspace with an AI agent, you can copy and paste the following initial prompt to bootstrap the agent.
76+
This instructs the agent to clone the repository, read coding guidelines/skills, and execute tasks accordingly:
77+
78+
.. code-block:: markdown
79+
80+
You are an AI coding assistant. Please follow these steps to set up our project workspace and solve my requests:
81+
82+
1. Run `git clone https://github.com/tensorcircuit/tensorcircuit-ng.git .` to download the repository in this empty folder.
83+
2. Read `AGENTS.md` at the root of the repository to understand the coding standards.
84+
3. Review the available agentic workflow scripts and skill files under `.agents/skills/` to see if a specialized workflow is suitable for the current task.
85+
4. Reference existing implementations in `examples/` and test cases in `tests/` to write differentiable, and JIT-friendly code.
86+
5. Respond to my later requests, write code, and execute experiments/tests to analyse the results.
87+
88+
Let me know when the initialization is complete and ask for my requests.
89+
90+
7291
AI-Native Documentation
7392
-----------------------
7493

0 commit comments

Comments
 (0)