Skip to content

Commit fb7285d

Browse files
authored
Improve ansatz generation API docs (#64)
* Improve ansatz generation API docs This also fixes #62, which is a one-line docs change. * Simplify example section * Add comment about abstract.py * fix typo * Address complaints from lint and sphinx
1 parent 30b683b commit fb7285d

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed

Diff for: docs/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,5 @@ Contents
4343
Explanatory Material <explanation/index>
4444
API Reference <apidocs/index>
4545
How-To Guides <how-tos/index>
46+
GitHub <https://github.com/Qiskit/qiskit-addon-aqc-tensor>
4647
Release Notes <release-notes>

Diff for: qiskit_addon_aqc_tensor/ansatz_generation.py

+56-3
Original file line numberDiff line numberDiff line change
@@ -121,20 +121,73 @@ def generate_ansatz_from_circuit(
121121
qubits_initially_zero: bool = False,
122122
parameter_name: str = "theta",
123123
) -> tuple[QuantumCircuit, list[float]]:
124-
"""Generate an ansatz from the two-qubit connectivity structure of a circuit.
124+
r"""Generate an ansatz from the two-qubit connectivity structure of a circuit.
125125
126-
See explanatatory material for motivation.
126+
See the `explanatatory material
127+
<https://qiskit.github.io/qiskit-addon-aqc-tensor/explanation/index.html#ansatz-generation-motivation>`__
128+
for motivation.
127129
128130
Args:
129131
qc: A circuit, which is assumed to be unitary. Barriers are ignored.
130132
qubits_initially_zero: If ``True``, the first Z rotation on each qubit
131-
is removed from the ansatz under the assumption that it has no effect.
133+
is fixed to zero because such a rotation has no effect on the state
134+
:math:`|0\rangle`.
132135
parameter_name: Name for the :class:`~qiskit.circuit.ParameterVector`
133136
representing the free parameters in the returned ansatz circuit.
134137
135138
Returns:
136139
``(ansatz, parameter_values)`` such that ``ansatz.assign_parameters(parameter_values)``
137140
is equivalent to ``qc`` up to a global phase.
141+
142+
Example:
143+
========
144+
145+
Consider the following circuit as an example:
146+
147+
.. plot::
148+
:alt: Circuit diagram output by the previous code.
149+
:include-source:
150+
:context: reset
151+
152+
from qiskit import QuantumCircuit
153+
154+
qc = QuantumCircuit(6)
155+
qc.rx(0.4, 0)
156+
qc.ryy(0.2, 2, 3)
157+
qc.h(2)
158+
qc.rz(0.1, 2)
159+
qc.rxx(0.3, 0, 1)
160+
qc.rzz(0.3, 0, 1)
161+
qc.cx(2, 1)
162+
qc.s(1)
163+
qc.h(4)
164+
qc.draw("mpl")
165+
166+
If the above circuit is passed to :func:`.generate_ansatz_from_circuit`, it will return an ansatz with parametrized two-qubit KAK rotations in the same locations as the input:
167+
168+
.. plot::
169+
:alt: Circuit diagram output by the previous code.
170+
:include-source:
171+
:context: close-figs
172+
173+
from qiskit_addon_aqc_tensor import generate_ansatz_from_circuit
174+
175+
ansatz, initial_params = generate_ansatz_from_circuit(
176+
qc, qubits_initially_zero=True, parameter_name="x"
177+
)
178+
ansatz.draw("mpl")
179+
180+
Note that in the generated ansatz, all consecutive single-qubit gates are collapsed into the same ZXZ block, and all consecutive two-qubit gates are collapsed into a single KAK block, up to single-qubit rotations.
181+
182+
Further, the :func:`.generate_ansatz_from_circuit` function provides parameters which, when bound to the ansatz, will result in a circuit equivalent to the original one, up to a global phase:
183+
184+
.. plot::
185+
:alt: Circuit diagram output by the previous code.
186+
:include-source:
187+
:context: close-figs
188+
189+
ansatz.assign_parameters(initial_params).draw("mpl")
190+
138191
"""
139192
# FIXME: handle classical bits, measurements, resets, and barriers. maybe
140193
# conditions too?

Diff for: qiskit_addon_aqc_tensor/simulation/abstract.py

+6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@
2020
from plum import dispatch
2121
from qiskit.circuit import Gate, QuantumCircuit
2222

23+
# NOTE: This file contains abstract classes and functions. The functions in
24+
# this file are implemented differently for each tensor-network backend, and
25+
# the backend method is chosen dynamically based on the type(s) passed to the
26+
# function. Dispatch is powered by plum-dispatch, a multiple dispatch library
27+
# for Python.
28+
2329

2430
class TensorNetworkState:
2531
"""Abstract tensor network state."""

0 commit comments

Comments
 (0)