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

SparsePauliOp.paulis setter does not propagate dimension info #10384

Closed
ihincks opened this issue Jul 4, 2023 · 3 comments · Fixed by #10437
Closed

SparsePauliOp.paulis setter does not propagate dimension info #10384

ihincks opened this issue Jul 4, 2023 · 3 comments · Fixed by #10437
Labels
bug Something isn't working mod: quantum info Related to the Quantum Info module (States & Operators)

Comments

@ihincks
Copy link
Contributor

ihincks commented Jul 4, 2023

Environment

  • Qiskit Terra version: 0.24.1
  • Python version: any
  • Operating system: any

What is happening?

Setting the paulis attribute (it has a @setter) on a SparsePauliOp instance does not update the BaseOperator shape attributes.

How can we reproduce the issue?

import qiskit.quantum_info as qi

pauli_x = qi.SparsePauliOp(["X"], [1])
pauli_x.paulis = ["XX"]
print(pauli_x, pauli_x.dim)

What should happen?

The above snippet produces the output

SparsePauliOp(['XX'],
              coeffs=[1.+0.j]) (2, 2)

whereas it should produce

SparsePauliOp(['XX'],
              coeffs=[1.+0.j]) (4, 4)

if changing the dimension is allowed, or raise an exception if it's not allowed.

Any suggestions?

I haven't looked closely, but it's possible that just updating _op_shape will be sufficient to fix the bug.

@ihincks ihincks added the bug Something isn't working label Jul 4, 2023
@atharva-satpute
Copy link
Contributor

atharva-satpute commented Jul 8, 2023

Looks like value of num_qubits gets updated from PauliList and up but does not get updated from SparsePauliOp. Correct me if I'm wrong

@woodsp-ibm woodsp-ibm added the mod: quantum info Related to the Quantum Info module (States & Operators) label Jul 13, 2023
@jakelishman
Copy link
Member

Personally I'd argue that those attributes shouldn't be settable by users at all, but I think the ship's sailed on that front. Given that the setter exists, I think the best solution would be to have it reject attempts to either modify the number of elements (so len(self._pauli_list) must not change) because that allows an inconsistency with self.coeffs, and also reject any attempt to modify the number of qubits because that just seems inconsistent with the rest of qiskit.quantum_info.

@ikkoham
Copy link
Contributor

ikkoham commented Jul 18, 2023

Agree with @jakelishman's comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working mod: quantum info Related to the Quantum Info module (States & Operators)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants