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.24k stars 2.36k forks source link

aqc synthesis plugin seems to ignore approximation_degree #10386

Open 1ucian0 opened 1 year ago

1ucian0 commented 1 year ago

The unitary synthesis plugin aqc gives always the same result, independently of approximation_degree.

Consider the following example:

# create a random unitary
from qiskit import QuantumCircuit
from qiskit.quantum_info import Operator
from qiskit.quantum_info.random import random_unitary

qc = QuantumCircuit(3)

qc.unitary(random_unitary(8, seed=0), [0,1,2])

# synthesis (with approximation_degree=0), back to matrix, and sum all the elements 

from qiskit.transpiler.passes.synthesis.unitary_synthesis import UnitarySynthesis
from qiskit.algorithms.optimizers import SLSQP
import numpy as np

result = UnitarySynthesis(basis_gates=["rx", "ry", "rz", "cx"], method='aqc', approximation_degree=0, plugin_config={"seed": 42})(qc)
approx_matrix = Operator(result).data

print(np.sum(approx_matrix))
(0.9471489692201448-0.04904005347947815j)

The result is the same with approximation_degree=1

result = UnitarySynthesis(basis_gates=["rx", "ry", "rz", "cx"], method='aqc', approximation_degree=1, plugin_config={"seed": 42})(qc)
approx_matrix = Operator(result).data

print(np.sum(approx_matrix))
(0.9471489692201448-0.04904005347947815j)

It would be great if the approximation_degree could affect the maxiter of the optimizer, for example (I'm not an expert in AQC).

For example, the following (with "optimizer": SLSQP(10)) takes 1.49s in my laptop.

qc = QuantumCircuit(4)
qc.unitary(random_unitary(16, seed=0), [0,1,2,3])

result = UnitarySynthesis(basis_gates=["rx", "ry", "rz", "cx"], method='aqc', plugin_config={"seed": 42, "optimizer": SLSQP(10)})(qc)

while the default one (I think is 100) takes 1m 53.1s

TL;DR Change default maxiter based on the approximation level, if not set. Maybe the amount of unitary boxes and qubits involved should play a role too.

SamD-1998 commented 1 year ago

Hi, I'd like to work on this issue. Can you assign it to me?

jakelishman commented 1 year ago

approximation_degree was somewhat deliberately not part of the unitary-synthesis plugin interface - it's a very very fuzzy heuristic dial for the default synthesis, and it doesn't make a huge amount of sense to give it special treatment and pass to other plugins. The way of specify pass-specific options to plugins is the plugin_config field you used in your second example.

Imo, the current system is working as expected, and the current interface is better than overloading the already near-meaningless approximation_degree option.

jakelishman commented 1 year ago

That said, it probably shoudl be clearer in the docstring of UnitarySynthesis that approximation_degree isn't part of the plugin interface, and only has meaning for the default synthesis (it only still exists at all for backwards compatibility).

SamD-1998 commented 1 year ago

So I gather this feature is not needed to be worked on at this time?

jakelishman commented 1 year ago

Well, Luciano might disagree with me (I'm not a dictator), but it's probably best to wait for his response at least.

SamD-1998 commented 1 year ago

As an aside: Is there an issue out there like this one which involves concepts of QM implemented in QC? Eg here was the adiabatic quantum computation and the adiabatic theorem. I find these interesting to work on!

jakelishman commented 1 year ago

We don't typically have a lot of feature requests destined for Terra in that sort of vein, because our current strategy is to ask interested people to contribute a package to the Qiskit ecosystem that implements a transpiler plugin. For example, the AQC pass mentioned in this issue is implemented as a transpiler plugin - even though it's within Terra (because it happened before we had the ecosystem properly), it registers itself using the same general mechanisms that all Python packages are free to use.

The documentation for the plugin interface is here, if you're interested: https://qiskit.org/documentation/apidoc/transpiler_synthesis_plugins.html

SamD-1998 commented 1 year ago

Gotcha! Even if they're transpiler plugins, that's fine with me! AQC sort of things just make things more interesting to me.

Anyway, I'll wait for @1ucian0 to respond.