cqlib-vqe 0.1.0b1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. cqlib_vqe-0.1.0b1/LICENSE +201 -0
  2. cqlib_vqe-0.1.0b1/MANIFEST.in +8 -0
  3. cqlib_vqe-0.1.0b1/PKG-INFO +336 -0
  4. cqlib_vqe-0.1.0b1/README.md +293 -0
  5. cqlib_vqe-0.1.0b1/README.zh-CN.md +237 -0
  6. cqlib_vqe-0.1.0b1/benchmarks/README.md +73 -0
  7. cqlib_vqe-0.1.0b1/benchmarks/run_cqlib_benchmark.py +1235 -0
  8. cqlib_vqe-0.1.0b1/cqlib_vqe/__init__.py +97 -0
  9. cqlib_vqe-0.1.0b1/cqlib_vqe/chemistry/__init__.py +47 -0
  10. cqlib_vqe-0.1.0b1/cqlib_vqe/chemistry/active_space.py +398 -0
  11. cqlib_vqe-0.1.0b1/cqlib_vqe/chemistry/mapping.py +187 -0
  12. cqlib_vqe-0.1.0b1/cqlib_vqe/chemistry/molecule.py +440 -0
  13. cqlib_vqe-0.1.0b1/cqlib_vqe/chemistry/uccsd.py +214 -0
  14. cqlib_vqe-0.1.0b1/cqlib_vqe/chemistry/uccsd_amplitudes.py +106 -0
  15. cqlib_vqe-0.1.0b1/cqlib_vqe/optim/__init__.py +34 -0
  16. cqlib_vqe-0.1.0b1/cqlib_vqe/optim/optim.py +619 -0
  17. cqlib_vqe-0.1.0b1/cqlib_vqe/utils/__init__.py +16 -0
  18. cqlib_vqe-0.1.0b1/cqlib_vqe/utils/profiler.py +55 -0
  19. cqlib_vqe-0.1.0b1/cqlib_vqe/vqe/__init__.py +65 -0
  20. cqlib_vqe-0.1.0b1/cqlib_vqe/vqe/adaptive.py +644 -0
  21. cqlib_vqe-0.1.0b1/cqlib_vqe/vqe/estimator.py +358 -0
  22. cqlib_vqe-0.1.0b1/cqlib_vqe/vqe/factory.py +543 -0
  23. cqlib_vqe-0.1.0b1/cqlib_vqe/vqe/hamiltonian.py +130 -0
  24. cqlib_vqe-0.1.0b1/cqlib_vqe/vqe/solver.py +296 -0
  25. cqlib_vqe-0.1.0b1/cqlib_vqe/vqe/tianyan.py +769 -0
  26. cqlib_vqe-0.1.0b1/cqlib_vqe.egg-info/PKG-INFO +336 -0
  27. cqlib_vqe-0.1.0b1/cqlib_vqe.egg-info/SOURCES.txt +59 -0
  28. cqlib_vqe-0.1.0b1/cqlib_vqe.egg-info/dependency_links.txt +1 -0
  29. cqlib_vqe-0.1.0b1/cqlib_vqe.egg-info/requires.txt +25 -0
  30. cqlib_vqe-0.1.0b1/cqlib_vqe.egg-info/top_level.txt +1 -0
  31. cqlib_vqe-0.1.0b1/examples/benchmark_execution_modes.py +100 -0
  32. cqlib_vqe-0.1.0b1/examples/h2_vqe_cqlib2.py +58 -0
  33. cqlib_vqe-0.1.0b1/examples/h2_vqe_tianyan.py +108 -0
  34. cqlib_vqe-0.1.0b1/examples/minimal_compiled_smoke.py +33 -0
  35. cqlib_vqe-0.1.0b1/examples/run_five_adaptive_full_pool.py +680 -0
  36. cqlib_vqe-0.1.0b1/examples/run_local_accuracy.py +297 -0
  37. cqlib_vqe-0.1.0b1/examples/tianyan_grouped_energy_smoke.py +76 -0
  38. cqlib_vqe-0.1.0b1/examples/tianyan_pauli_strict_probe.py +116 -0
  39. cqlib_vqe-0.1.0b1/examples/tianyan_qwc_compare.py +132 -0
  40. cqlib_vqe-0.1.0b1/pyproject.toml +72 -0
  41. cqlib_vqe-0.1.0b1/setup.cfg +4 -0
  42. cqlib_vqe-0.1.0b1/tests/conftest.py +278 -0
  43. cqlib_vqe-0.1.0b1/tests/test_active_space_and_canonical_ucc.py +198 -0
  44. cqlib_vqe-0.1.0b1/tests/test_adaptive_full_pool.py +286 -0
  45. cqlib_vqe-0.1.0b1/tests/test_api_check_tool.py +19 -0
  46. cqlib_vqe-0.1.0b1/tests/test_cqlib2_adapter.py +232 -0
  47. cqlib_vqe-0.1.0b1/tests/test_estimator_cache.py +52 -0
  48. cqlib_vqe-0.1.0b1/tests/test_mapper_consistency.py +112 -0
  49. cqlib_vqe-0.1.0b1/tests/test_optimizer_regressions.py +250 -0
  50. cqlib_vqe-0.1.0b1/tests/test_public_api.py +33 -0
  51. cqlib_vqe-0.1.0b1/tests/test_qwc.py +24 -0
  52. cqlib_vqe-0.1.0b1/tests/test_tianyan.py +321 -0
  53. cqlib_vqe-0.1.0b1/tools/benchmark_native_pauli_rotation.py +126 -0
  54. cqlib_vqe-0.1.0b1/tools/check_cqlib2_api.py +86 -0
  55. cqlib_vqe-0.1.0b1/tools/check_source_sync.py +57 -0
  56. cqlib_vqe-0.1.0b1/tools/check_tianyan_api.py +109 -0
  57. cqlib_vqe-0.1.0b1/tools/diagnose_initial_point.py +118 -0
  58. cqlib_vqe-0.1.0b1/tools/diagnose_zero_amplitude_pool.py +133 -0
  59. cqlib_vqe-0.1.0b1/tools/tianyan_raw_result_probe.py +125 -0
  60. cqlib_vqe-0.1.0b1/tools/validate_chemistry_correctness.py +111 -0
  61. cqlib_vqe-0.1.0b1/tools/verify_native_pauli_rotation.py +94 -0
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2026 China Telecom Quantum Group
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,8 @@
1
+ include LICENSE
2
+ include README.md
3
+ include README.zh-CN.md
4
+ recursive-include examples *.py
5
+ recursive-include tools *.py
6
+ recursive-include benchmarks *.py *.md
7
+ recursive-include scripts *.sh
8
+ recursive-include tests *.py
@@ -0,0 +1,336 @@
1
+ Metadata-Version: 2.4
2
+ Name: cqlib-vqe
3
+ Version: 0.1.0b1
4
+ Summary: Adaptive canonical singlet-UCCSD and active-space VQE for cqlib 2.x
5
+ Author: Cqlib VQE Contributors
6
+ License-Expression: Apache-2.0
7
+ Keywords: quantum computing,variational quantum eigensolver,VQE,UCCSD,quantum chemistry,cqlib
8
+ Classifier: Development Status :: 4 - Beta
9
+ Classifier: Operating System :: OS Independent
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: Programming Language :: Python :: 3 :: Only
12
+ Classifier: Programming Language :: Python :: 3.10
13
+ Classifier: Programming Language :: Python :: 3.11
14
+ Classifier: Programming Language :: Python :: 3.12
15
+ Classifier: Programming Language :: Python :: 3.13
16
+ Classifier: Programming Language :: Python :: 3.14
17
+ Classifier: Topic :: Scientific/Engineering
18
+ Classifier: Topic :: Scientific/Engineering :: Chemistry
19
+ Requires-Python: >=3.10
20
+ Description-Content-Type: text/markdown
21
+ License-File: LICENSE
22
+ Requires-Dist: numpy>=2.1
23
+ Requires-Dist: scipy>=1.11
24
+ Requires-Dist: cqlib>=2.0.0b1
25
+ Provides-Extra: chemistry
26
+ Requires-Dist: openfermion>=1.6; extra == "chemistry"
27
+ Requires-Dist: openfermionpyscf>=0.5; extra == "chemistry"
28
+ Requires-Dist: pyscf>=2.6; extra == "chemistry"
29
+ Provides-Extra: tianyan
30
+ Requires-Dist: cqlib-tianyan>=0.1.0b1; extra == "tianyan"
31
+ Provides-Extra: test
32
+ Requires-Dist: pytest>=8; extra == "test"
33
+ Provides-Extra: dev
34
+ Requires-Dist: build>=1.2; extra == "dev"
35
+ Requires-Dist: pytest>=8; extra == "dev"
36
+ Requires-Dist: ruff>=0.6; extra == "dev"
37
+ Provides-Extra: all
38
+ Requires-Dist: openfermion>=1.6; extra == "all"
39
+ Requires-Dist: openfermionpyscf>=0.5; extra == "all"
40
+ Requires-Dist: pyscf>=2.6; extra == "all"
41
+ Requires-Dist: cqlib-tianyan>=0.1.0b1; extra == "all"
42
+ Dynamic: license-file
43
+
44
+ # cqlib-vqe
45
+
46
+ [简体中文](README.zh-CN.md)
47
+
48
+ `cqlib-vqe` is a VQE package for molecular electronic-structure calculations on
49
+ cqlib 2.x. It provides molecular-Hamiltonian preparation, canonical
50
+ singlet-UCCSD ansätze, Jordan-Wigner (JW), Bravyi-Kitaev (BK), and parity
51
+ encodings, adaptive operator-pool selection, local statevector execution, and
52
+ Tianyan cloud/hardware measurement support.
53
+
54
+ The primary public import is `cqlib_vqe`.
55
+
56
+ ## What the package provides
57
+
58
+ - Molecular preprocessing through PySCF and OpenFermion, including a
59
+ reproducible active-space report and Pauli Hamiltonian.
60
+ - Canonical singlet-UCCSD generators with a stable packed-parameter ordering.
61
+ - Consistent JW, BK, and parity treatment of the Hamiltonian, excitation
62
+ generators, and Hartree-Fock reference state.
63
+ - `DirectStatevectorEstimator` for local high-throughput VQE objectives and
64
+ `NativeStatevectorEstimator` for circuit/statevector expectation values.
65
+ - Complete-pool adaptive selected-UCCSD: a zero CCSD initial amplitude does
66
+ not remove an operator from the candidate pool.
67
+ - QWC-grouped Tianyan measurement plans and strict validation of submitted-task
68
+ and result-qubit metadata.
69
+
70
+ ## Installation
71
+
72
+ Clone the repository and create an isolated Python environment. Python 3.10 or
73
+ newer is required.
74
+
75
+ ```bash
76
+ git clone https://github.com/cq-lib/cqlib-vqe.git
77
+ cd cqlib-vqe
78
+
79
+ python -m venv .venv
80
+ source .venv/bin/activate # Windows PowerShell: .\.venv\Scripts\Activate.ps1
81
+ python -m pip install --upgrade pip
82
+ ```
83
+
84
+ Install the variant required by the workflow:
85
+
86
+ ```bash
87
+ # Local VQE from a precompiled ansatz.
88
+ python -m pip install -e .
89
+
90
+ # Molecular Hamiltonian and UCCSD preprocessing.
91
+ python -m pip install -e ".[chemistry]"
92
+
93
+ # Tianyan execution in addition to molecular preprocessing.
94
+ python -m pip install -e ".[chemistry,tianyan]"
95
+
96
+ # Development and test dependencies.
97
+ python -m pip install -e ".[chemistry,tianyan,test]"
98
+ ```
99
+
100
+ Run the complete test suite after installation:
101
+
102
+ ```bash
103
+ pytest -q
104
+ ```
105
+
106
+ ## Quick start: a precompiled one-parameter VQE
107
+
108
+ Use `UCCSDFactory.from_compiled` when a Pauli-generator ansatz is already
109
+ available and molecular preprocessing is intentionally not part of the runtime.
110
+ Each generator is a list of `(pauli_string, coefficient)` terms. Pauli strings
111
+ use the package's legacy `q0-left` order: the leftmost character acts on qubit
112
+ 0.
113
+
114
+ ```python
115
+ from cqlib_vqe import DirectStatevectorEstimator, UCCSDFactory, VQESolver
116
+
117
+ # G = i * 0.5 * Y; the one-qubit Hamiltonian is H = Z.
118
+ factory = UCCSDFactory.from_compiled(
119
+ n_qubits=1,
120
+ n_electrons=0,
121
+ generators=[[('Y', 0.5)]],
122
+ initial_values=[0.1],
123
+ construction_mode='jit',
124
+ )
125
+
126
+ estimator = DirectStatevectorEstimator(n_qubits=1)
127
+ solver = VQESolver(
128
+ factory,
129
+ estimator,
130
+ optimizer_method='COBYLA',
131
+ max_iter=30,
132
+ execution_mode='auto',
133
+ )
134
+ result = solver.run([('Z', 1.0)])
135
+
136
+ print(result['optimal_value'])
137
+ print(result['optimal_params'])
138
+ ```
139
+
140
+ `solver.run(...)` returns a dictionary containing `optimal_value`,
141
+ `optimal_params`, `n_evals`, `n_iters`, `success`, `message`,
142
+ `execution_mode`, and the complete energy `history`.
143
+
144
+ This smoke example deliberately caps COBYLA at 30 evaluations. It can therefore
145
+ reach the numerical minimum while still returning `success=False` because the
146
+ evaluation budget is exhausted. Increase `max_iter` and define an
147
+ energy/parameter convergence criterion appropriate to the production study.
148
+
149
+ The runnable version is available as:
150
+
151
+ ```bash
152
+ python examples/minimal_compiled_smoke.py
153
+ ```
154
+
155
+ ## Molecular VQE workflow
156
+
157
+ For a molecular calculation, first build `MolecularDataEngine`. Its `run()`
158
+ method performs the electronic-structure preprocessing and exposes
159
+ `n_qubits`, `n_electrons`, `hamiltonian_data`, CCSD initial amplitudes, and
160
+ reference energies. Then construct the UCCSD circuit factory, choose an
161
+ estimator, and start `VQESolver`.
162
+
163
+ ```python
164
+ from cqlib_vqe import (
165
+ DirectStatevectorEstimator,
166
+ MolecularDataEngine,
167
+ UCCSDFactory,
168
+ VQESolver,
169
+ )
170
+
171
+ molecule = MolecularDataEngine(
172
+ geometry=[
173
+ ('H', (0.0, 0.0, 0.0)),
174
+ ('H', (0.0, 0.0, 0.735)),
175
+ ],
176
+ basis='sto-3g',
177
+ multiplicity=1,
178
+ charge=0,
179
+ mapper_type='jw',
180
+ excitation_threshold=1e-3,
181
+ ).run()
182
+
183
+ factory = UCCSDFactory(
184
+ molecule,
185
+ construction_mode='jit',
186
+ trotter_steps=1,
187
+ trotter_order=2,
188
+ )
189
+ estimator = DirectStatevectorEstimator(n_qubits=molecule.n_qubits)
190
+ solver = VQESolver(
191
+ factory,
192
+ estimator,
193
+ optimizer_method='COBYLA',
194
+ max_iter=100,
195
+ tol=1e-7,
196
+ execution_mode='auto',
197
+ )
198
+
199
+ def progress(_parameters, energy, evaluation):
200
+ if evaluation == 1 or evaluation % 10 == 0:
201
+ print(f'eval={evaluation:4d} energy={energy:.12f} Ha')
202
+
203
+ result = solver.run(molecule.hamiltonian_data, callback=progress)
204
+ print('VQE energy:', result['optimal_value'])
205
+ print('reference energies:', molecule.reference_energies)
206
+ ```
207
+
208
+ Run the maintained H2 example directly with:
209
+
210
+ ```bash
211
+ python examples/h2_vqe_cqlib2.py
212
+ ```
213
+
214
+ ### Choosing an execution mode
215
+
216
+ `execution_mode='auto'` is the recommended local default. It chooses the
217
+ fused direct-statevector path when the factory and estimator support it, and
218
+ otherwise uses circuit execution. Use `execution_mode='circuit'` when a
219
+ circuit-oriented estimator is required, for example a cloud estimator. Use
220
+ `execution_mode='direct_statevector'` only when both
221
+ `factory.build_statevector(...)` and `estimator.evaluate_parameters(...)` are
222
+ available.
223
+
224
+ `construction_mode='jit'` builds numeric circuits for repeated evaluation.
225
+ `construction_mode='bind'` keeps a symbolic template and is useful when the
226
+ target execution path benefits from parameter binding.
227
+
228
+ ## Mapping choice and active spaces
229
+
230
+ Set `mapper_type` to one of `"jw"`, `"bk"`, or `"parity"` when constructing
231
+ `MolecularDataEngine`:
232
+
233
+ ```python
234
+ molecule = MolecularDataEngine(
235
+ geometry=[('H', (0.0, 0.0, 0.0)), ('H', (0.0, 0.0, 0.735))],
236
+ basis='sto-3g',
237
+ mapper_type='bk',
238
+ ).run()
239
+ ```
240
+
241
+ The selected mapping is applied consistently to the molecular Hamiltonian,
242
+ the UCCSD excitation generators, and the Hartree-Fock reference. In BK and
243
+ parity encodings, the reference computational-basis bit pattern is not in
244
+ general characterized by its Hamming weight alone; do not replace the factory
245
+ reference construction with a JW occupancy shortcut.
246
+
247
+ For larger systems, set an explicit active-space policy before running the
248
+ molecule engine. The qubit budget must be positive and even.
249
+
250
+ ```python
251
+ from cqlib_vqe import ActiveSpaceConfig, MolecularDataEngine
252
+
253
+ active_space = ActiveSpaceConfig.automatic(
254
+ max_active_qubits=10,
255
+ freeze_core=True,
256
+ min_virtual_orbitals=1,
257
+ )
258
+ molecule = MolecularDataEngine(
259
+ geometry=[('Li', (0.0, 0.0, 0.0)), ('H', (0.0, 0.0, 1.596))],
260
+ basis='sto-3g',
261
+ mapper_type='jw',
262
+ active_space=active_space,
263
+ ).run()
264
+
265
+ print(molecule.active_space_report)
266
+ ```
267
+
268
+ Use `ActiveSpaceConfig.full()` to retain the full orbital space. The active
269
+ space report should be recorded with every numerical result because it fixes
270
+ the simulated Hamiltonian and parameter pool.
271
+
272
+ ## Adaptive selected-UCCSD
273
+
274
+ The adaptive solver initializes an ansatz from canonical CCSD amplitudes and
275
+ then scans the complete remaining canonical operator pool. Candidates with
276
+ zero initial CCSD amplitude remain eligible and are ranked by their measured
277
+ energy gradients.
278
+
279
+ The five-molecule runner provides an auditable end-to-end workflow. Start with
280
+ H2 and a small number of pool scans:
281
+
282
+ ```bash
283
+ python examples/run_five_adaptive_full_pool.py h2 \\
284
+ --output-dir run_outputs/h2_adaptive \\
285
+ --pool-rounds 2 \\
286
+ --maxiter 80
287
+ ```
288
+
289
+ The output directory contains per-molecule logs and JSON records, plus a CSV
290
+ and JSON summary. The runner accepts `h2`, `h4`, `lih`, `beh2`, and `h2o`; omit
291
+ the positional molecule list to run all five. Use `--active-space-policy full`
292
+ only when the full orbital space is intended and the available resources are
293
+ adequate.
294
+
295
+ ## Tianyan execution
296
+
297
+ Tianyan runs submit real measurement tasks and may consume platform credits.
298
+ Install the `tianyan` extra, configure credentials outside version control, and
299
+ inspect the available backend names before submitting a VQE job:
300
+
301
+ ```bash
302
+ export TIANYAN_API_KEY='YOUR_API_KEY'
303
+ python tools/check_tianyan_api.py --online
304
+ ```
305
+
306
+ Then select a backend and physical-qubit mapping. The mapping is ordered from
307
+ logical qubits to physical qubits and must contain exactly `n_qubits` entries.
308
+
309
+ ```bash
310
+ export TIANYAN_DEVICE='BACKEND_NAME'
311
+ export TIANYAN_PHYSICAL_QUBITS='0,1,2,3'
312
+ export TIANYAN_SHOTS=2000
313
+ export TIANYAN_MAXITER=10
314
+ export TIANYAN_CALIBRATION=disabled
315
+ python -u examples/h2_vqe_tianyan.py
316
+ ```
317
+
318
+ `TianyanEnergyEstimator` groups qubit-wise commuting Pauli terms before
319
+ submission and validates task ordering, measured qubit headers, and count
320
+ payloads on return. The estimator does not infer routing or silently repair
321
+ backend metadata. Never commit API keys, credential files, or raw cloud output
322
+ that contains account information.
323
+
324
+ ## Useful entry points
325
+
326
+ | Task | Command |
327
+ | --- | --- |
328
+ | Minimal precompiled VQE | `python examples/minimal_compiled_smoke.py` |
329
+ | Local H2 UCCSD-VQE | `python examples/h2_vqe_cqlib2.py` |
330
+ | Adaptive H2 workflow | `python examples/run_five_adaptive_full_pool.py h2` |
331
+ | Tianyan interface check | `python tools/check_tianyan_api.py --online` |
332
+ | Full test suite | `pytest -q` |
333
+
334
+ ## License
335
+
336
+ Apache License 2.0. See [LICENSE](LICENSE).