Qiskit / qiskit-tutorials

A collection of Jupyter notebooks showing how to use the Qiskit SDK
Apache License 2.0
2.32k stars 1.29k forks source link

Quantum circuit generated by Traveling Salesman Problem raise an error: "Image size of 2228x191753 pixels is too large" #1461

Closed OuCheng-Fu closed 1 year ago

OuCheng-Fu commented 1 year ago

Informations

What is the current behavior?

 I use the source code of qiskit_optimization.applications Tsp module to solve the Traveling Salesman Problem. I use a 6-vertex graph as example

6 verteices Then I apply the source code of quadratic program to construct a QUBO problem with this code

rom qiskit_optimization import QuadraticProgram from qiskit_optimization.translators import from_docplex_mp from docplex.mp.model import Model

mdl = Model(name="TSP") n = G.number_of_nodes() x = {(i, k): mdl.binaryvar(name=f"x{i}_{k}") for i in range(n) for k in range(n)}

tsp_func = mdl.sum( 1.0 x[(i, k)] x[(j, (k + 1) % n)] for i in range(n) for j in range(n) for k in range(n) if i != j)

mdl.minimize(tsp_func) for i in range(n): mdl.add_constraint(mdl.sum(x[(i, k)] for k in range(n)) == 1) for k in range(n): mdl.add_constraint(mdl.sum(x[(i, k)] for i in range(n)) == 1) qp = from_docplex_mp(mdl)

print(qp.prettyprint())

After that, I get the result Minimize x_0_0x_1_1 + x_0_0x_1_5 + x_0_0x_2_1 + x_0_0x_2_5 + x_0_0*x_3_1

Subject to Linear constraints (12) x_0_0 + x_0_1 + x_0_2 + x_0_3 + x_0_4 + x_0_5 == 1 'c0' x_1_0 + x_1_1 + x_1_2 + x_1_3 + x_1_4 + x_1_5 == 1 'c1' x_2_0 + x_2_1 + x_2_2 + x_2_3 + x_2_4 + x_2_5 == 1 'c2' x_3_0 + x_3_1 + x_3_2 + x_3_3 + x_3_4 + x_3_5 == 1 'c3' x_4_0 + x_4_1 + x_4_2 + x_4_3 + x_4_4 + x_4_5 == 1 'c4' x_5_0 + x_5_1 + x_5_2 + x_5_3 + x_5_4 + x_5_5 == 1 'c5' x_0_0 + x_1_0 + x_2_0 + x_3_0 + x_4_0 + x_5_0 == 1 'c6' x_0_1 + x_1_1 + x_2_1 + x_3_1 + x_4_1 + x_5_1 == 1 'c7' x_0_2 + x_1_2 + x_2_2 + x_3_2 + x_4_2 + x_5_2 == 1 'c8' x_0_3 + x_1_3 + x_2_3 + x_3_3 + x_4_3 + x_5_3 == 1 'c9' x_0_4 + x_1_4 + x_2_4 + x_3_4 + x_4_4 + x_5_4 == 1 'c10' x_0_5 + x_1_5 + x_2_5 + x_3_5 + x_4_5 + x_5_5 == 1 'c11'

Binary variables (36) x_0_0 x_0_1 x_0_2 x_0_3 x_0_4 x_0_5 x_1_0 x_1_1 x_1_2 x_1_3 x_1_4 x_1_5 x_2_0 x_2_1 x_2_2 x_2_3 x_2_4 x_2_5 x_3_0 x_3_1 x_3_2 x_3_3 x_3_4 x_3_5 x_4_0 x_4_1 x_4_2 x_4_3 x_4_4 x_4_5 x_5_0 x_5_1 x_5_2 x_5_3 x_5_4 x_5_5

Then I try to turn it into Ising model problem by using the code from qiskit_optimization.converters import QuadraticProgramToQubo qp2qubo = QuadraticProgramToQubo() qubo = qp2qubo.convert(qp) qubitOp, offset = qubo.to_ising() and do trotter evolution of it (visualize it into quantum circuit)

U_hcp = (theta*qubitOp).exp_i()

Trotter Evolution

from qiskit.opflow import PauliTrotterEvolution, Suzuki trot_op_hcp = PauliTrotterEvolution(trotter_mode=Suzuki(order=1, reps=1)).convert(U_hcp) trot_op_hcp.to_circuit().draw(output='mpl', plot_barriers=False)

As a result, an error message raises File ~/.conda/envs/py2023/lib/python3.11/site-packages/matplotlib/backends/backend_agg.py:84, in RendererAgg.init(self, width, height, dpi) 82 self.width = width 83 self.height = height ---> 84 self._renderer = _RendererAgg(int(width), int(height), dpi) 85 self._filter_renderers = [] 87 self._update_methods()

ValueError: Image size of 2228x191753 pixels is too large. It must be less than 2^16 in each direction.

Steps to reproduce the problem

Implement traveling salesman problem of a graph more than 6 vertices, and draw the circuit of its Ising model Trotter Evolution.

What is the expected behavior?

The circuit should have been displayed, despite being lengthy.

Suggested solutions

How can I display the circuit by shrinking its size or enlarge the displaying pixels of python?

jaydity commented 1 year ago

I guess you can use scale = 0.x

OuCheng-Fu commented 1 year ago

I guess you can use scale = 0.x

Thank you. I used scale=0.1 and it succeeded.