Qiskit / qiskit-aer

Aer is a high performance simulator for quantum circuits that includes noise models
https://qiskit.github.io/qiskit-aer/
Apache License 2.0
497 stars 361 forks source link

Performance difference for generating the unitary of a circuit using AerSimulator vs quantum_info.Operator #2170

Open ACE07-Sev opened 4 months ago

ACE07-Sev commented 4 months ago

Informations

Greetings there,

Hope all are well. I had a quick question I was hoping to ask. I understand there are two ways we can generate the unitary of a circuit. One is using qiskit.quantum_info.Operator, and the other is using AerSimulator like so:

# Construct quantum circuit without measure
circ = QuantumCircuit(2)
circ.h(0)
circ.cx(0, 1)
circ.save_unitary()

# Transpile for simulator
simulator = AerSimulator(method = 'unitary')
circ = transpile(circ, simulator)

# Run and get unitary
result = simulator.run(circ).result()
unitary = result.get_unitary(circ)
print("Circuit unitary:\n", np.asarray(unitary).round(5))

I was wondering what the main difference between the two approaches is, and if someone wants to generate the unitary (which is admittedly an expensive operation as we have to contract all the gates together) if they can do so using GPU (and whether this is then only doable with AerSimulator and thus the difference).

doichanj commented 4 months ago

I think it is better to use qiskit.quantum_info for smaller number of qubits, but Aer is suitable to make large unitary matrix, because Aer has some overheads. I think if you want to make > 7qubits unitary matrix, Aer will be faster

ACE07-Sev commented 4 months ago

I see. Would I be able to get the unitary matrix using GPU as a device when using Aer?

doichanj commented 4 months ago

Yes, you can use GPU by setting option device=GPU