I would like to use a general entangler in the entanglement_block of the TwoLocal function. The function seems to work with 'cz', 'cx', 'swap' gates, but it fails for 'iswap' gate. Below is the error message. Any help would be appreciated

from qiskit.circuit.library import TwoLocal
ansatz = TwoLocal(num_spin_orbitals, ['ry', 'rz'], entanglement_blocks = 'iswap', entanglement='linear')

Gives the following error

Traceback (most recent call last):
  File "<ipython-input-34-5d8e95fd9811>", line 2, in <module>
    ansatz = TwoLocal(num_spin_orbitals, ['ry', 'rz'], entanglement_blocks = 'iswap', entanglement='linear')
  File "/opt/conda/lib/python3.8/site-packages/qiskit/circuit/library/n_local/two_local.py", line 177, in __init__
  File "/opt/conda/lib/python3.8/site-packages/qiskit/circuit/library/n_local/n_local.py", line 140, in __init__
    self.entanglement_blocks = entanglement_blocks
  File "/opt/conda/lib/python3.8/site-packages/qiskit/circuit/library/n_local/n_local.py", line 248, in entanglement_blocks
    self._entanglement_blocks = [self._convert_to_block(block) for block in blocks]
  File "/opt/conda/lib/python3.8/site-packages/qiskit/circuit/library/n_local/n_local.py", line 248, in <listcomp>
    self._entanglement_blocks = [self._convert_to_block(block) for block in blocks]
  File "/opt/conda/lib/python3.8/site-packages/qiskit/circuit/library/n_local/two_local.py", line 248, in _convert_to_block
    raise ValueError(f'Unknown layer name `{layer}`.') from ex
ValueError: Unknown layer name `iswap`.

1 Answer 1



In Qiskit 1.1, all the standard gates in qiskit.circuit.library can be specified by string name for the entangling operations in TwoLocal circuits. Which means, the code snippet in the question should work now with no issue.

The Old Answer:

Although the documentation says that the entanglement_blocks parameter can be specified via the name of a gate or the gate type itself, not all gates can be used this way. You can check the code to get the list of supported gate names.

Since "pass by name" is not supported for iSWAP, you can use the gate type:

ansatz = TwoLocal(num_spin_orbitals, ['ry', 'rz'], entanglement_blocks = iSwapGate(), entanglement='linear')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.