Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
axz60 opened this issue Jan 9, 2025 · 6 comments · Fixed by #13634
Labels
bug Something isn't working

Comments

@axz60
Copy link

axz60 commented Jan 9, 2025

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 :)

@nihaocami
Copy link

Right I believe the wrong parameters are being passed here:

data = pauli_evolution(num_qubits, expanded_paulis, insert_barriers, False)

As the pauli_evolution function in Rust expects different ones ?

pub fn pauli_evolution<'a>(
pauli: &'a str,
indices: Vec<u32>,
time: Param,
phase_gate: bool,
do_fountain: bool,

Not a 100% sure. Maybe someone from the team has a better explanation.

@axz60 axz60 changed the title Hamiltoninan variational ansatz not being about to deal with pauli strings only containing identity operators Hamiltoninan variational ansatz not working when considering Pauli-strings with only identity Pauli operators Jan 10, 2025
@axz60 axz60 changed the title Hamiltoninan variational ansatz not working when considering Pauli-strings with only identity Pauli operators Hamiltoninan variational ansatz not working when considering Pauli-strings with only identity operators Jan 10, 2025
@Cryoris
Copy link
Contributor

Cryoris commented Jan 10, 2025

This is a duplicate of #13625 and will be fixed by #13634.

@Cryoris Cryoris added the bug Something isn't working label Jan 10, 2025
@axz60
Copy link
Author

axz60 commented Jan 10, 2025

Thank very much @nihaocami and @Cryoris for having a look!

@axz60
Copy link
Author

axz60 commented Jan 14, 2025

To implement the new changes, do I pip install qiskit again?

@Cryoris
Copy link
Contributor

Cryoris commented Jan 14, 2025

We have a bugfix release planned next week, once that is released you will be able to install qiskit==1.3.2. Until then, you'd have to install Qiskit from source to have the changes locally. See https://github.com/Qiskit/qiskit/blob/main/CONTRIBUTING.md#installing-qiskit-from-source for instructions on how to do that.

@axz60
Copy link
Author

axz60 commented Jan 14, 2025

Cheers! Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants