-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Closed
Labels
bugSomething isn't workingSomething isn't working
Description
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
Labels
bugSomething isn't workingSomething isn't working