Skip to content

Conversation

yaelbh
Copy link
Contributor

@yaelbh yaelbh commented May 4, 2025

Summary

Addresses part of #14129

Details and comments

Added the following methods to ObservablesArray:

  • apply_layout
  • equivalent
  • copy

Also added SparseObservable to ObservableLike and a test to verify the workflow with the estimator.

@yaelbh yaelbh requested review from a team as code owners May 4, 2025 14:18
@qiskit-bot
Copy link
Collaborator

One or more of the following people are relevant to this code:

  • @Qiskit/terra-core
  • @ajavadia
  • @levbishop
  • @t-imamichi

@coveralls
Copy link

coveralls commented May 4, 2025

Pull Request Test Coverage Report for Build 14892081928

Details

  • 19 of 19 (100.0%) changed or added relevant lines in 1 file are covered.
  • 15 unchanged lines in 3 files lost coverage.
  • Overall coverage decreased (-0.01%) to 87.833%

Files with Coverage Reduction New Missed Lines %
crates/accelerate/src/unitary_synthesis.rs 1 94.85%
crates/qasm2/src/parse.rs 6 97.15%
crates/qasm2/src/lex.rs 8 91.98%
Totals Coverage Status
Change from base Build 14884993559: -0.01%
Covered Lines: 74535
Relevant Lines: 84860

💛 - Coveralls

for ndi, obs in np.ndenumerate(self._array):
new_arr[ndi] = obs.apply_layout(layout, num_qubits)

return ObservablesArray(new_arr)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I chose not to set validate to False, only to be on the safe side, but I actually don't see a real reason. What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should not validate in the constructor when making new instances in method implementations

Copy link
Contributor

@ihincks ihincks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @yaelbh !

arr2 = np.atleast_1d(other._array).flat

for obs1, obs2 in zip(arr1, arr2):
if (obs1 - obs2).simplify(tol) != SparseObservable.zero(self.num_qubits):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: the creation of the zero array can be moved outside of the loop.


n_qubits = self.num_qubits
if isinstance(layout, TranspileLayout):
n_qubits = len(layout._output_qubit_list)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't be using private attributes of other classes. Is there another way to fetch this information?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like just len(layout) is sufficient?

for ndi, obs in np.ndenumerate(self._array):
new_arr[ndi] = obs.apply_layout(layout, num_qubits)

return ObservablesArray(new_arr)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should not validate in the constructor when making new instances in method implementations

ihincks
ihincks previously approved these changes May 7, 2025
@yaelbh yaelbh added this pull request to the merge queue May 8, 2025
Merged via the queue into Qiskit:main with commit a43fdd8 May 8, 2025
24 checks passed
@eliarbel eliarbel added this to the 2.1.0 milestone Jun 3, 2025
@eliarbel eliarbel added the Changelog: New Feature Include in the "Added" section of the changelog label Jun 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: New Feature Include in the "Added" section of the changelog
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants