Qiskit / qiskit

Qiskit is an open-source SDK for working with quantum computers at the level of extended quantum circuits, operators, and primitives.
https://www.ibm.com/quantum/qiskit
Apache License 2.0
5.23k stars 2.36k forks source link

Matrix representation of circuit #3011

Closed eddieschoute closed 3 years ago

eddieschoute commented 5 years ago

What is the expected enhancement?

It would be great to be able to inspect the unitary matrix of a circuit that can be represented as such. Currently one can go through Operator to get a representation but it's not obvious to a user how to do this. A circuit.to_matrix method or similar would make this much clearer and easily accessible. However, not all circuits have a matrix representation and thus this would not be universal.

chriseclectic commented 5 years ago

I don't think this should be a circuit method:

It would be better to better document to the user how to use the Operator class or unitary simulator to do this

rafamartinc commented 5 years ago

I think that matrix representations can be very useful during the development of an algorithm, for small sets of qubits, and also during the first exercises of a student that is learning how quantum gates alter the states. At least, for me it was :)

Documenting it would make it less accesible for new developers, I think it is something that should be available for use in the first tutorials about quantum gates. So, implementing it and discouraging its use under the circumstances you describe, would help advanced users understand when it should no longer be used.

Just my opinion though :)

ajavadia commented 5 years ago

This is already available in the Operator class. It is one extra line of code:

from qiskit.quantum_info import Operator
Operator(circuit)

but it does proper error handling, ability to further analyze that operator, compose two circuits via composing their operators, etc.

So I don't think this shortcut should exist; we should encourage the use of Qiskit classes. This probably points to the Operator class not having been exposed to users as well as the QuantumCircuit class. This should be fixed via tutorials, etc.

eddieschoute commented 5 years ago

Yes, it's available but both me and a collaborator did not find it on our own so something is lacking. I'm also not sure how constructing an Operator would be better than constructing an Operator inside a to_matrix method, though the former is a bit more specific with regards to what's happening.

rafamartinc commented 5 years ago

Also, if the feature is so hidden that requires a tutorial for users to know about it and how to use it, maybe a modification is indeed required :) Plus, the feature is almost developed and ready for review, I only have to find time to fix a circular dependency. https://github.com/Qiskit/qiskit-terra/pull/3110

Cryoris commented 3 years ago

Is this issue still active? FWIW I agree with @chriseclectic's answer above, I think it would be better to not hide a potentially expensive and not scalable operation in a seemingly innocent circuit method 🙂

Also, getting the matrix is now in the basic circuit tutorial: https://qiskit.org/documentation/tutorials/circuits/01_circuit_basics.html#Unitary-representation-of-a-circuit.

1ucian0 commented 3 years ago

closing this issues as wont fix. Please, reopen if there are new arguments for a QuantumCircuit.to_matrix method.

Zshan0 commented 1 year ago

The operator approach generates the matrix but it also permutes the qubit order. Afaik there is no way of setting the qubit order to match the convention. I have illustrated the problem I have encountered here, the solution is specific to attaining the controlled gate but there is no general approach.

There needs to be an inherent way of generating the matrix of a QuantumCircuit without relying on Operator as an intermediate.

jakelishman commented 1 year ago

Qiskit's endianness convention is well-defined and common in programming; Operator doesn't permute the qubits, it just respects the little-endian convention that Qiskit uses. If you want to reverse the bit order in a circuit such that calling Operator(circuit) produces a matrix in apparent big-endian convention, you can use QuantumCircuit.reverse_bits() first, or you can use Operator.reverse_qargs() after the operator is created. (Alternatively, you can also just use a little-endian convention in your matrix work if you're using Qiskit, or you'll always be fighting our convention.)

Generating a matrix from a circuit somewhat efficiently (if it's even possible) is a very non-trivial operation, and given that we've supported Operator doing that, I stand by the arguments that were made two and four years ago about not adding another way of doing it.