Skip to content
This repository has been archived by the owner on Dec 7, 2021. It is now read-only.

Use Operator rather than unitary simulator to convert circuit to unitary matrix #1224

Merged
merged 7 commits into from
Sep 12, 2020

Conversation

jlapeyre
Copy link
Contributor

@jlapeyre jlapeyre commented Aug 30, 2020

Summary

Closes #1218

This PR makes (for example) qiskit.aqua.operators.Z.exp_i().to_matrix() return the correct matrix. It does two things:

  • Z.exp_i() previously returned a circuit containing a Z-rotation gate, but with angle incorrect by a factor of 2. This PR uses the correct factor. The PR corrects the same erroneous angle for X and Y rotations.
  • The conversion of CircuitOp to a matrix used the Aer unitary simulator. It returns the wrong matrix by a global phase. This is probably due to rough edges in the new global phase support in Terra. qiskit.quantum_info.Operator will also convert a circuit to a matrix, if possible. It does account for the global_phase property.

This PR still lacks a test and release note.
EDIT: tests have been added.
EDIT: release note has been adde.

@jlapeyre jlapeyre requested a review from Cryoris August 30, 2020 18:03
@jlapeyre jlapeyre force-pushed the pauli-expi branch 3 times, most recently from 39cda1b to 466bd48 Compare August 31, 2020 16:56
The unitary simulator does not account for the global phase in a
circuit. qiskit.quantum_info.Operator does account for global phase
when converting (when possible) a circuit to a unitary matrix.

Closes qiskit-community#1218
@jlapeyre jlapeyre changed the title [WIP] Use Operator rather than unitary simulator to convert circuit to unitary matrix Use Operator rather than unitary simulator to convert circuit to unitary matrix Sep 4, 2020
@jlapeyre
Copy link
Contributor Author

jlapeyre commented Sep 4, 2020

This should be GTG.

Cryoris
Cryoris previously approved these changes Sep 4, 2020
Copy link
Contributor

@Cryoris Cryoris left a comment

Choose a reason for hiding this comment

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

LGTM 👍🏼 about the TODO/NOTE comments, are they still open or could
you check if they can be removed?

@jlapeyre
Copy link
Contributor Author

jlapeyre commented Sep 4, 2020

about the TODO/NOTE comments, are they still open

I'll check. This should probably be resolved before committing.

@woodsp-ibm woodsp-ibm added the Changelog: Bugfix Include in the Fixed section of the changelog label Sep 5, 2020
@woodsp-ibm woodsp-ibm added this to the 0.8 milestone Sep 5, 2020
@t-imamichi
Copy link
Contributor

t-imamichi commented Sep 9, 2020

We discussed BasicAer in the past https://github.com/Qiskit/qiskit-aqua/issues/1003. I remember there were some concerns. Is it clear now? I made a PoC for the discussion #1009.

@woodsp-ibm
Copy link
Member

@t-imamichi Are you asking is it clear about the removal of BasicAer etc as was originally discussed in the issue and PoC PR? To my knowledge nothing further has happened. This PR is just about addressing some internal logic, where the issue as stated is incorrect global phase in the result - I think internal use within Aqua was aspect 2 in your original issue - where it uses BasicAer say for statevector or unitary conversion. Maybe more internal aspects could be changed if it was lower overhead, better performance etc. This was one such internal use where the outcome was just not correct and noticed as part of the phase estimation work and needed to be addressed.

@t-imamichi
Copy link
Contributor

Thanks. I got it.

@jlapeyre
Copy link
Contributor Author

Sorry, @t-imamichi I missed your comment till just now.

  • I didn't make the connection between your #1003 and the present PR until now.
  • I'm not sure what your question here is. But, maybe this will help make it more clear: The use of BasicAer here resulted in an incorrect matrix. I was looking for the most straightforward way to fix this. Browsing around some comments in Terra, it seemed to me that using quantum_info.Operator is the currently sanctioned way to convert a unitary circuit to a matrix. I tried this, and it also had the happy consequence of returning the correct matrix. I haven't thought about whether replacing BasicAer more broadly, as in your PoC, is a good idea at the moment. But, unless there is something I am overlooking (which might be) it seems that making the change here is a good idea, because it fixes a bug, and fits with direction we want to take Terra and Aqua in any case.

@jlapeyre jlapeyre requested a review from levbishop as a code owner September 11, 2020 17:42
@jlapeyre
Copy link
Contributor Author

jlapeyre commented Sep 11, 2020

LGTM 👍🏼 about the TODO/NOTE comments, are they still open or could
you check if they can be removed?

Yes, they can be removed.
I checked in a case in which BasicAer does give the correct result Z ^ Y. The two methods give the same matrix: $Z \otimes Y$. Printing the circuit shows Y on qubit 0 and Z on qubit 1.

EDIT: The comments have been removed

Using Operator(QuantumCircuit) gives the same qubit ordering
that the previous unitary-simulator-based code did. I removed
the comment entirely because the behavior depends only on
Operator(QuantumCircuit), which is not obscure.
@t-imamichi
Copy link
Contributor

@jlapeyre Sorry for confusing you. This PR does not have the problem we discussed in #1003.

@jlapeyre jlapeyre merged commit f059f99 into qiskit-community:master Sep 12, 2020
jlapeyre added a commit to jlapeyre/qiskit-aqua that referenced this pull request Sep 15, 2020
…ary matrix (qiskit-community#1224)

* Fix factor of 2 error in converting exp_i to rotation gates

* Make CircuitOp use Operator rather than unitary simulator to get matrix

The unitary simulator does not account for the global phase in a
circuit. qiskit.quantum_info.Operator does account for global phase
when converting (when possible) a circuit to a unitary matrix.

Closes qiskit-community#1218

* Remove note on reversing order of qubits

Using Operator(QuantumCircuit) gives the same qubit ordering
that the previous unitary-simulator-based code did. I removed
the comment entirely because the behavior depends only on
Operator(QuantumCircuit), which is not obscure.
pbark pushed a commit to pbark/qiskit-aqua that referenced this pull request Sep 16, 2020
…ary matrix (qiskit-community#1224)

* Fix factor of 2 error in converting exp_i to rotation gates

* Make CircuitOp use Operator rather than unitary simulator to get matrix

The unitary simulator does not account for the global phase in a
circuit. qiskit.quantum_info.Operator does account for global phase
when converting (when possible) a circuit to a unitary matrix.

Closes qiskit-community#1218

* Remove note on reversing order of qubits

Using Operator(QuantumCircuit) gives the same qubit ordering
that the previous unitary-simulator-based code did. I removed
the comment entirely because the behavior depends only on
Operator(QuantumCircuit), which is not obscure.
mtreinish pushed a commit to mtreinish/qiskit-core that referenced this pull request Nov 20, 2020
…ary matrix (qiskit-community/qiskit-aqua#1224)

* Fix factor of 2 error in converting exp_i to rotation gates

* Make CircuitOp use Operator rather than unitary simulator to get matrix

The unitary simulator does not account for the global phase in a
circuit. qiskit.quantum_info.Operator does account for global phase
when converting (when possible) a circuit to a unitary matrix.

Closes qiskit-community/qiskit-aqua#1218

* Remove note on reversing order of qubits

Using Operator(QuantumCircuit) gives the same qubit ordering
that the previous unitary-simulator-based code did. I removed
the comment entirely because the behavior depends only on
Operator(QuantumCircuit), which is not obscure.
manoelmarques pushed a commit to manoelmarques/qiskit-terra that referenced this pull request Dec 2, 2020
…ary matrix (qiskit-community/qiskit-aqua#1224)

* Fix factor of 2 error in converting exp_i to rotation gates

* Make CircuitOp use Operator rather than unitary simulator to get matrix

The unitary simulator does not account for the global phase in a
circuit. qiskit.quantum_info.Operator does account for global phase
when converting (when possible) a circuit to a unitary matrix.

Closes qiskit-community/qiskit-aqua#1218

* Remove note on reversing order of qubits

Using Operator(QuantumCircuit) gives the same qubit ordering
that the previous unitary-simulator-based code did. I removed
the comment entirely because the behavior depends only on
Operator(QuantumCircuit), which is not obscure.
manoelmarques pushed a commit to manoelmarques/qiskit-terra that referenced this pull request Dec 7, 2020
…ary matrix (qiskit-community/qiskit-aqua#1224)

* Fix factor of 2 error in converting exp_i to rotation gates

* Make CircuitOp use Operator rather than unitary simulator to get matrix

The unitary simulator does not account for the global phase in a
circuit. qiskit.quantum_info.Operator does account for global phase
when converting (when possible) a circuit to a unitary matrix.

Closes qiskit-community/qiskit-aqua#1218

* Remove note on reversing order of qubits

Using Operator(QuantumCircuit) gives the same qubit ordering
that the previous unitary-simulator-based code did. I removed
the comment entirely because the behavior depends only on
Operator(QuantumCircuit), which is not obscure.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Changelog: Bugfix Include in the Fixed section of the changelog
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Z.exp_i().to_matrix() disagrees with documented behavior
4 participants