Skip to content

Hamiltoninan variational ansatz not working when considering Pauli-strings with only identity operators #13644

@axz60

Description

@axz60

Environment

  • Qiskit version: 1.3.1
  • Python version: 3.12.7
  • Operating system: macOS 14.6.1 (23G93) Darwin 23.6.0

What is happening?
The hamiltonian variational ansatz can't produce circuits for pauli strings only containing identity operators.

How can we reproduce the issue?

Code to reproduce the error:

from qiskit.quantum_info import SparsePauliOp
from qiskit.circuit.library import hamiltonian_variational_ansatz

# this Hamiltonian will be split into the two terms [III, IZZ] and [IXI]

hamiltonian = SparsePauliOp(["III", "IZZ", "IXI"])
ansatz = hamiltonian_variational_ansatz(hamiltonian, reps=2)
ansatz.draw("mpl")

Error Generated:


	"name": "PanicException",
	"message": "internal error: entered unreachable code",
	"stack": "---------------------------------------------------------------------------
PanicException                            Traceback (most recent call last)
Cell In[110], line 6
      4 # this Hamiltonian will be split into the two terms [III, IZZ] and [IXI]
      5 hamiltonian = SparsePauliOp([\"III\", \"IZZ\", \"IXI\"])
----> 6 ansatz = hamiltonian_variational_ansatz(hamiltonian, reps=2)
      7 ansatz.draw(\"mpl\")

File /opt/anaconda3/envs/hva_test/lib/python3.12/site-packages/qiskit/circuit/library/n_local/evolved_operator_ansatz.py:265, in hamiltonian_variational_ansatz(hamiltonian, reps, insert_barriers, name, parameter_prefix)
    262 if isinstance(hamiltonian, SparsePauliOp):
    263     hamiltonian = hamiltonian.group_commuting()
--> 265 return evolved_operator_ansatz(
    266     hamiltonian,
    267     reps=reps,
    268     evolution=None,
    269     insert_barriers=insert_barriers,
    270     name=name,
    271     parameter_prefix=parameter_prefix,
    272     flatten=True,
    273 )

File /opt/anaconda3/envs/hva_test/lib/python3.12/site-packages/qiskit/circuit/library/n_local/evolved_operator_ansatz.py:141, in evolved_operator_ansatz(operators, reps, evolution, insert_barriers, name, parameter_prefix, remove_identities, flatten)
    136         param = next(param_iter)
    137         expanded_paulis += [
    138             (pauli, indices, 2 * coeff * param) for pauli, indices, coeff in term
    139         ]
--> 141 data = pauli_evolution(num_qubits, expanded_paulis, insert_barriers, False)
    142 circuit = QuantumCircuit._from_circuit_data(data, add_regs=True)
    143 circuit.name = name

PanicException: internal error: entered unreachable code"
}

Why does the error occur?

pauli_evolution(...) function takes a sparse list of pauli operators like so,

sparse_list = hamiltonian.to_sparse_list()
print(sparse_list)

#output
[(' ', [ ], np.complex128(1+0j)), ('ZZ', [0, 1], np.complex128(1+0j)), ('X', [1], np.complex128(1+0j))]

However if a pauli string only contains identity operators, they are defined by an empty string and array, which 'pauli_evolution(...)' can't handle.

The question
Is there anyway of circumventing this issue?

PS: Apologies if my syntaxing of this issue is incorrect, this is the first time I have raised an issue on github :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions