---
title: ansatz_generation (latest version)
description: API reference for qiskit_addon_aqc_tensor.ansatz_generation in the latest version of qiskit-addon-aqc-tensor
source: https://quantum.cloud.ibm.com/docs/en/api/qiskit-addon-aqc-tensor/ansatz-generation
---

# Ansatz generation

`qiskit_addon_aqc_tensor.ansatz_generation`

Tools for generating ansatz circuits.

|                                                                                                                                                                     |                                                        |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------ |
| [`AnsatzBlock`](/docs/api/qiskit-addon-aqc-tensor/ansatz-generation-ansatz-block "qiskit_addon_aqc_tensor.ansatz_generation.AnsatzBlock")                           | Ansatz block.                                          |
| [`OneQubitAnsatzBlock`](/docs/api/qiskit-addon-aqc-tensor/ansatz-generation-one-qubit-ansatz-block "qiskit_addon_aqc_tensor.ansatz_generation.OneQubitAnsatzBlock") | One-qubit ansatz block.                                |
| [`TwoQubitAnsatzBlock`](/docs/api/qiskit-addon-aqc-tensor/ansatz-generation-two-qubit-ansatz-block "qiskit_addon_aqc_tensor.ansatz_generation.TwoQubitAnsatzBlock") | Two-qubit ansatz block.                                |
| [`ZXZ`](/docs/api/qiskit-addon-aqc-tensor/ansatz-generation-zxz "qiskit_addon_aqc_tensor.ansatz_generation.ZXZ")                                                    | One-qubit ansatz block based on the ZXZ decomposition. |
| [`KAK`](/docs/api/qiskit-addon-aqc-tensor/ansatz-generation-kak "qiskit_addon_aqc_tensor.ansatz_generation.KAK")                                                    | Two-qubit ansatz block based on the KAK decomposition. |

### generate\_ansatz\_from\_circuit

`generate_ansatz_from_circuit(qc, /, *, qubits_initially_zero=False, parameter_name='theta')`

[GitHub](https://github.com/Qiskit/qiskit-addon-aqc-tensor/tree/stable/0.3/qiskit_addon_aqc_tensor/ansatz_generation/from_connectivity.py#L164-L422)

Generate an ansatz from the two-qubit connectivity structure of a circuit.

See the [explanatatory material](https://qiskit.github.io/qiskit-addon-aqc-tensor/explanation/index.html#ansatz-generation-motivation) for motivation.

**Parameters**

- **qc** ([`QuantumCircuit`](/docs/api/qiskit/qiskit.circuit.QuantumCircuit)) – A circuit, which is assumed to be unitary. Barriers are ignored.
- **qubits\_initially\_zero** ([`bool`](https://docs.python.org/3/library/functions.html#bool)) – If `True`, the first Z rotation on each qubit is fixed to zero because such a rotation has no effect on the state $|0\rangle$.
- **parameter\_name** ([`str`](https://docs.python.org/3/library/stdtypes.html#str)) – Name for the [`ParameterVector`](/docs/api/qiskit/qiskit.circuit.ParameterVector) representing the free parameters in the returned ansatz circuit.

**Return type**

[`tuple`](https://docs.python.org/3/library/stdtypes.html#tuple)\[[`QuantumCircuit`](/docs/api/qiskit/qiskit.circuit.QuantumCircuit), [`list`](https://docs.python.org/3/library/stdtypes.html#list)\[[`float`](https://docs.python.org/3/library/functions.html#float)]]

**Returns**

`(ansatz, parameter_values)` such that `ansatz.assign_parameters(parameter_values)` is equivalent to `qc` up to a global phase.

#### Example:

Consider the following circuit as an example:

```python
from qiskit import QuantumCircuit

qc = QuantumCircuit(6)
qc.rx(0.4, 0)
qc.ryy(0.2, 2, 3)
qc.h(2)
qc.rz(0.1, 2)
qc.rxx(0.3, 0, 1)
qc.rzz(0.3, 0, 1)
qc.cx(2, 1)
qc.s(1)
qc.h(4)
qc.draw("mpl")
```

![Circuit diagram output by the previous code.](https://quantum.cloud.ibm.com/docs/images/api/qiskit-addon-aqc-tensor/ansatz_generation-1.svg)

If the above circuit is passed to [`generate_ansatz_from_circuit()`](#qiskit_addon_aqc_tensor.ansatz_generation.generate_ansatz_from_circuit "qiskit_addon_aqc_tensor.ansatz_generation.generate_ansatz_from_circuit"), it will return an ansatz with parametrized two-qubit KAK rotations in the same locations as the input:

```python
from qiskit_addon_aqc_tensor import generate_ansatz_from_circuit

ansatz, initial_params = generate_ansatz_from_circuit(
    qc, qubits_initially_zero=True, parameter_name="x"
)
ansatz.draw("mpl")
```

![Circuit diagram output by the previous code.](https://quantum.cloud.ibm.com/docs/images/api/qiskit-addon-aqc-tensor/ansatz_generation-2.svg)

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.

Further, the [`generate_ansatz_from_circuit()`](#qiskit_addon_aqc_tensor.ansatz_generation.generate_ansatz_from_circuit "qiskit_addon_aqc_tensor.ansatz_generation.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:

```python
ansatz.assign_parameters(initial_params).draw("mpl")
```

![Circuit diagram output by the previous code.](https://quantum.cloud.ibm.com/docs/images/api/qiskit-addon-aqc-tensor/ansatz_generation-3.svg)

A 1D Trotter circuit leads to a similar result, with its characteristic brickwork structure:

```python
from rustworkx.generators import path_graph
from qiskit.synthesis import SuzukiTrotter
from qiskit_addon_utils.problem_generators import generate_time_evolution_circuit, generate_xyz_hamiltonian

hamiltonian = generate_xyz_hamiltonian(
    path_graph(6),
    coupling_constants=(0.0, 0.0, 1.0),
    ext_magnetic_field=(0.4, 0.0, 0.0),
)

good_circuit = generate_time_evolution_circuit(
    hamiltonian,
    synthesis=SuzukiTrotter(reps=2),
    time=1.0,
)

good_circuit.draw("mpl", initial_state=True)
```

![Circuit diagram output by the previous code.](https://quantum.cloud.ibm.com/docs/images/api/qiskit-addon-aqc-tensor/ansatz_generation-4.svg)

```python
from qiskit_addon_aqc_tensor import generate_ansatz_from_circuit

ansatz, initial_params = generate_ansatz_from_circuit(
    good_circuit, qubits_initially_zero=True, parameter_name="x"
)
ansatz.assign_parameters(initial_params).draw("mpl", initial_state=True)
```

![Circuit diagram output by the previous code.](https://quantum.cloud.ibm.com/docs/images/api/qiskit-addon-aqc-tensor/ansatz_generation-5.svg)

### parametrize\_circuit

`parametrize_circuit(qc, /, *, parameter_name='theta')`

[GitHub](https://github.com/Qiskit/qiskit-addon-aqc-tensor/tree/stable/0.3/qiskit_addon_aqc_tensor/ansatz_generation/parametrize_circuit.py#L22-L151)

Create a parametrized version of a circuit.

Given a quantum circuit, constructs another quantum circuit which is identical except that any gates with numerical parameters are replaced by gates (of the same type) with free parameters. The new circuit is returned along with a list containing the original values of the parameters.

**Parameters**

- **qc** ([`QuantumCircuit`](/docs/api/qiskit/qiskit.circuit.QuantumCircuit)) – The quantum circuit to parametrize.
- **parameter\_name** ([`str`](https://docs.python.org/3/library/stdtypes.html#str)) – Name for the [`ParameterVector`](/docs/api/qiskit/qiskit.circuit.ParameterVector) representing the free parameters in the returned ansatz circuit.

**Return type**

[`tuple`](https://docs.python.org/3/library/stdtypes.html#tuple)\[[`QuantumCircuit`](/docs/api/qiskit/qiskit.circuit.QuantumCircuit), [`list`](https://docs.python.org/3/library/stdtypes.html#list)\[[`float`](https://docs.python.org/3/library/functions.html#float) | [`None`](https://docs.python.org/3/library/constants.html#None)]]

**Returns**

`(ansatz, parameter_values)` such that `ansatz.assign_parameters(parameter_values)` is identical to `qc` as long as `qc` did not already contain parameters. If `qc` already had parameters, then `parameter_values` will contain `None` at the entries corresponding to those parameters.

#### Example:

Consider the following circuit as an example:

```python
from qiskit import QuantumCircuit

qc = QuantumCircuit(6)
qc.rx(0.4, 0)
qc.ryy(0.2, 2, 3)
qc.h(2)
qc.rz(0.1, 2)
qc.rxx(0.3, 0, 1)
qc.rzz(0.3, 0, 1)
qc.cx(2, 1)
qc.s(1)
qc.h(4)
qc.draw("mpl")
```

![Circuit diagram output by the previous code.](https://quantum.cloud.ibm.com/docs/images/api/qiskit-addon-aqc-tensor/ansatz_generation-6.svg)

If the above circuit is passed to [`parametrize_circuit()`](#qiskit_addon_aqc_tensor.ansatz_generation.parametrize_circuit "qiskit_addon_aqc_tensor.ansatz_generation.parametrize_circuit"), it will return an ansatz obtained from this circuit by replacing numerical parameters with free parameters:

```python
from qiskit_addon_aqc_tensor import parametrize_circuit

ansatz, initial_params = parametrize_circuit(qc)
ansatz.draw("mpl")
```

![Circuit diagram output by the previous code.](https://quantum.cloud.ibm.com/docs/images/api/qiskit-addon-aqc-tensor/ansatz_generation-7.svg)

Further, the [`parametrize_circuit()`](#qiskit_addon_aqc_tensor.ansatz_generation.parametrize_circuit "qiskit_addon_aqc_tensor.ansatz_generation.parametrize_circuit") function provides parameters which, when bound to the ansatz, will result in a circuit identical to the original one:

```python
ansatz.assign_parameters(initial_params).draw("mpl")
```

![Circuit diagram output by the previous code.](https://quantum.cloud.ibm.com/docs/images/api/qiskit-addon-aqc-tensor/ansatz_generation-8.svg)

If the original circuit already contained parameters, then the returned parameter values will contain `None` at the entries corresponding to those parameters, and the preceding code will not work. The following example shows how to recover the original circuit in this case.

```python
from qiskit.circuit import Parameter

qc = QuantumCircuit(3)
alpha1 = Parameter("alpha1")
alpha2 = Parameter("alpha2")
qc.ry(alpha1, [0])
qc.rz(0.1, [0])
qc.ry(alpha2, [1])
qc.rz(alpha1, [1])
qc.ry(0.2, [2])
qc.rz(0.3, [2])
ansatz, initial_params = parametrize_circuit(qc)
ansatz.assign_parameters(
    {
        param: val
        for param, val in zip(ansatz.parameters, initial_params)
        if val is not None
    },
    inplace=True,
)
ansatz.draw("mpl")
```

![Circuit diagram output by the previous code.](https://quantum.cloud.ibm.com/docs/images/api/qiskit-addon-aqc-tensor/ansatz_generation-9.svg)
