Row 4458

Row ID: 4458 | Dataset Entry | Axioma AXP Content Repository

Content Data

This page contains data entry 4458 from the Axioma AXP content repository. The structured data below represents the complete record for this entry.

Trying to implement Traveling Salesman problem solution using Quantum service via vscode and python, but stuck on this error. Full code given below:

import matplotlib.pyplot as plt import networkx as nx from qiskit.circuit.library import TwoLocal from qiskit_optimization.applications import Tsp from qiskit_algorithms.optimizers import SPSA from qiskit_algorithms.utils import algorithm_globals from qiskit_optimization.converters import QuadraticProgramToQubo from qiskit_ibm_runtime import SamplerV2 as Sampler from qiskit_ibm_runtime import QiskitRuntimeService def draw_graph(G, colors, pos): default_axes = plt.axes(frameon=True) nx.draw_networkx(G, node_color=colors, node_size=600, alpha=0.8, ax=default_axes, pos=pos) edge_labels = nx.get_edge_attributes(G, "weight") nx.draw_networkx_edge_labels(G, pos=pos, edge_labels=edge_labels) def draw_tsp_solution(G, order, colors, pos): G2 = nx.DiGraph() G2.add_nodes_from(G) n = len(order) for i in range(n): j = (i + 1) % n G2.add_edge(order[i], order[j], weight=G[order[i]][order[j]]["weight"]) default_axes = plt.axes(frameon=True) nx.draw_networkx( G2, node_color=colors, edge_color="b", node_size=600, alpha=0.8, ax=default_axes, pos=pos ) edge_labels = nx.get_edge_attributes(G2, "weight") nx.draw_networkx_edge_labels(G2, pos, font_color="b", edge_labels=edge_labels) # Generating a graph of 3 nodes n = 3 tsp = Tsp.create_random_instance(n, seed=123) adj_matrix = nx.to_numpy_array(tsp.graph) print("distance\n", adj_matrix) colors = ["r" for node in tsp.graph.nodes] pos = [tsp.graph.nodes[node]["pos"] for node in tsp.graph.nodes] draw_graph(tsp.graph, colors, pos) qp = tsp.to_quadratic_program() print(qp.prettyprint()) qp2qubo = QuadraticProgramToQubo() qubo = qp2qubo.convert(qp) qubitOp, offset = qubo.to_ising() print("Offset:", offset) print("Ising Hamiltonian:") print(str(qubitOp)) algorithm_globals.random_seed = 123 seed = 10598 optimizer = SPSA(maxiter=300) ry = TwoLocal(qubitOp.num_qubits, "ry", "cz", reps=5, entanglement="linear") # For an IBM Quantum account. ibm_quantum_service = QiskitRuntimeService(channel="ibm_quantum", token="xxxxx") service = QiskitRuntimeService() #Optimize problem for quantum execution. backend = service.least_busy(operational=True, simulator=False) # Define the QuantumCircuit from PauliSumOp qubit_circuit = qubitOp.to_circuit() sampler = Sampler(backend=backend) sampler.options.default_shots = 1024 # Options can be set using auto-complete. result = sampler.run(qubit_circuit) print("energy:", result.eigenvalue.real) print("time:", result.optimizer_time) x = tsp.sample_most_likely(result.eigenstate) z = tsp.interpret(x) print("solution:", z) print("solution objective:", tsp.tsp_value(z, adj_matrix)) draw_tsp_solution(tsp.graph, z, colors, pos) print(f"Job ID is {result.job_id()}")

The output I'm getting is given below.

(new_qiskit_env) PS F:\XXXXX> & f:/XXXX/new_qiskit_env/Scripts/python.exe f:/XXXXXXX/tsp_qc_ibm distance [[ 0. 48. 91.] [48. 0. 63.] [91. 63. 0.]] Problem name: TSP Minimize 48*x_0_0*x_1_1 + 48*x_0_0*x_1_2 + 91*x_0_0*x_2_1 + 91*x_0_0*x_2_2 + 48*x_0_1*x_1_0 + 48*x_0_1*x_1_2 + 91*x_0_1*x_2_0 + 91*x_0_1*x_2_2 + 48*x_0_2*x_1_0 + 48*x_0_2*x_1_1 + 91*x_0_2*x_2_0 + 91*x_0_2*x_2_1 + 63*x_1_0*x_2_1 + 63*x_1_0*x_2_2 + 63*x_1_1*x_2_0 + 63*x_1_1*x_2_2 + 63*x_1_2*x_2_0 + 63*x_1_2*x_2_1 Subject to Linear constraints (6) x_0_0 + x_0_1 + x_0_2 == 1 'c0' x_1_0 + x_1_1 + x_1_2 == 1 'c1' x_2_0 + x_2_1 + x_2_2 == 1 'c2' x_0_0 + x_1_0 + x_2_0 == 1 'c3' x_0_1 + x_1_1 + x_2_1 == 1 'c4' x_0_2 + x_1_2 + x_2_2 == 1 'c5' Binary variables (9) x_0_0 x_0_1 x_0_2 x_1_0 x_1_1 x_1_2 x_2_0 x_2_1 x_2_2 Offset: 7581.0 Ising Hamiltonian: SparsePauliOp(['IIIIIIIIZ', 'IIIIIIIZI', 'IIIIIIZII', 'IIIIIZIII', 'IIIIZIIII', 'IIIZIIIII', 'IIZIIIIII', 'IZIIIIIII', 'ZIIIIIIII', 'IIIIIIIZZ', 'IIIIIIZIZ', 'IIIIIZIIZ', 'IIIIZIIIZ', 'IIIZIIIIZ', 'IIZIIIIIZ', 'IZIIIIIIZ', 'ZIIIIIIIZ', 'IIIIIIZZI', 'IIIIIZIZI', 'IIIIZIIZI', 'IIIZIIIZI', 'IIZIIIIZI', 'IZIIIIIZI', 'ZIIIIIIZI', 'IIIIIZZII', 'IIIIZIZII', 'IIIZIIZII', 'IIZIIIZII', 'IZIIIIZII', 'ZIIIIIZII', 'IIIIZZIII', 'IIIZIZIII', 'IIZIIZIII', 'IZIIIZIII', 'ZIIIIZIII', 'IIIZZIIII', 'IIZIZIIII', 'IZIIZIIII', 'ZIIIZIIII', 'IIZZIIIII', 'IZIZIIIII', 'ZIIZIIIII', 'IZZIIIIII', 'ZIZIIIIII', 'ZZIIIIIII'], coeffs=[-1282.5 +0.j, -1282.5 +0.j, -1282.5 +0.j, -1268.5 +0.j, -1268.5 +0.j, -1268.5 +0.j, -1290. +0.j, -1290. +0.j, -1290. +0.j, 606.5 +0.j, 606.5 +0.j, 606.5 +0.j, 12. +0.j, 12. +0.j, 606.5 +0.j, 22.75+0.j, 22.75+0.j, 606.5 +0.j, 12. +0.j, 606.5 +0.j, 12. +0.j, 22.75+0.j, 606.5 +0.j, 22.75+0.j, 12. +0.j, 12. +0.j, 606.5 +0.j, 22.75+0.j, 22.75+0.j, 606.5 +0.j, 606.5 +0.j, 606.5 +0.j, 606.5 +0.j, 15.75+0.j, 15.75+0.j, 606.5 +0.j, 15.75+0.j, 606.5 +0.j, 15.75+0.j, 15.75+0.j, 15.75+0.j, 606.5 +0.j, 606.5 +0.j, 606.5 +0.j, 606.5 +0.j]) Traceback (most recent call last): File "f:\XXXXX\tsp_qc_ibm", line 77, in <module> qubit_circuit = qubitOp.to_circuit() ^^^^^^^^^^^^^^^^^^ AttributeError: 'SparsePauliOp' object has no attribute 'to_circuit'

If I dont use the to\_circuit() and try to pass the problem directly to the sampler, the following error occurs.

result = sampler.run(qubitOp)

Error.

raise TypeError("circuit must be QuantumCircuit.") TypeError: circuit must be QuantumCircuit.

FieldValue
text Trying to implement Traveling Salesman problem solution using Quantum service via vscode and python, but stuck on this error. Full code given below: import matplotlib.pyplot as plt import networkx as nx from qiskit.circuit.library import TwoLocal from qiskit_optimization.applications import Tsp from qiskit_algorithms.optimizers import SPSA from qiskit_algorithms.utils import algorithm_globals from qiskit_optimization.converters import QuadraticProgramToQubo …
label r/quantumcomputing
dataType post
communityName r/QuantumComputing
datetime 2024-04-12
username_encoded Z0FBQUFBQm5LakwxQVpITWhYai1LMDdlWHYzVHNtd2V0VEtsZVY3NndJN1YyS0FJMzVMYTB0b3dXSlBkVm5xNDRGeTRBczh5VDFYWHdhSDV2ei1KUkN5dG1LVm1HSmhwMVE9PQ==
url_encoded Z0FBQUFBQm5Lak9GY1dkYVJtUzBYV2x6WFV1YldMLWJvaDBBV1MtMWIyQklZZjRldjI2RXFEN2UyUS1XN1BFdE41OHNIcmtaZjFKSXVwLUJGa2JMRnBsWmJsMUJHa2x5TTM0RWhYRFQ2Z1lGQWR5MkdSUnZ4QnhMWC0tWGgxVHZGWG5fX2ozMXVkZkdwdl9nczJwd3dGTUVSNURMRnlUaWVQMTZnalBnYXprVGgwTnQ4Q09wZ2toR1FFZGlFSWQ0bDhrZktoX3hTY2RDVUd3TWhMMlpwTDI3MEwyMlpSbDgxUT09

Raw Record

{
  "text": "Trying to implement Traveling Salesman problem solution using Quantum service via vscode and python, but stuck on this error. Full code given below:\n\n    import matplotlib.pyplot as plt\n    import networkx as nx\n    \n    from qiskit.circuit.library import TwoLocal\n    from qiskit_optimization.applications import Tsp\n    from qiskit_algorithms.optimizers import SPSA\n    from qiskit_algorithms.utils import algorithm_globals\n    from qiskit_optimization.converters import QuadraticProgramToQubo\n    from qiskit_ibm_runtime import SamplerV2 as Sampler\n    \n    from qiskit_ibm_runtime import QiskitRuntimeService\n    \n    \n    def draw_graph(G, colors, pos):\n        default_axes = plt.axes(frameon=True)\n        nx.draw_networkx(G, node_color=colors, node_size=600, alpha=0.8, ax=default_axes, pos=pos)\n        edge_labels = nx.get_edge_attributes(G, \"weight\")\n        nx.draw_networkx_edge_labels(G, pos=pos, edge_labels=edge_labels)\n    \n    def draw_tsp_solution(G, order, colors, pos):\n        G2 = nx.DiGraph()\n        G2.add_nodes_from(G)\n        n = len(order)\n        for i in range(n):\n            j = (i + 1) % n\n            G2.add_edge(order[i], order[j], weight=G[order[i]][order[j]][\"weight\"])\n        default_axes = plt.axes(frameon=True)\n        nx.draw_networkx(\n            G2, node_color=colors, edge_color=\"b\", node_size=600, alpha=0.8, ax=default_axes, pos=pos\n        )\n        edge_labels = nx.get_edge_attributes(G2, \"weight\")\n        nx.draw_networkx_edge_labels(G2, pos, font_color=\"b\", edge_labels=edge_labels)\n    \n    # Generating a graph of 3 nodes\n    n = 3\n    tsp = Tsp.create_random_instance(n, seed=123)\n    adj_matrix = nx.to_numpy_array(tsp.graph)\n    print(\"distance\\n\", adj_matrix)\n    \n    colors = [\"r\" for node in tsp.graph.nodes]\n    pos = [tsp.graph.nodes[node][\"pos\"] for node in tsp.graph.nodes]\n    draw_graph(tsp.graph, colors, pos)\n    \n    qp = tsp.to_quadratic_program()\n    print(qp.prettyprint())\n    \n    qp2qubo = QuadraticProgramToQubo()\n    qubo = qp2qubo.convert(qp)\n    qubitOp, offset = qubo.to_ising()\n    print(\"Offset:\", offset)\n    print(\"Ising Hamiltonian:\")\n    print(str(qubitOp))\n    \n    algorithm_globals.random_seed = 123\n    seed = 10598\n    \n    optimizer = SPSA(maxiter=300)\n    ry = TwoLocal(qubitOp.num_qubits, \"ry\", \"cz\", reps=5, entanglement=\"linear\")\n    \n    \n    # For an IBM Quantum account.\n    ibm_quantum_service = QiskitRuntimeService(channel=\"ibm_quantum\", token=\"xxxxx\")\n    \n    service = QiskitRuntimeService()\n    \n    #Optimize problem for quantum execution.\n    backend = service.least_busy(operational=True, simulator=False)\n    \n    # Define the QuantumCircuit from PauliSumOp\n    qubit_circuit = qubitOp.to_circuit()\n    \n    sampler = Sampler(backend=backend)\n    sampler.options.default_shots = 1024  # Options can be set using auto-complete.\n    \n    result = sampler.run(qubit_circuit)\n    \n    print(\"energy:\", result.eigenvalue.real)\n    print(\"time:\", result.optimizer_time)\n    x = tsp.sample_most_likely(result.eigenstate)\n    z = tsp.interpret(x)\n    print(\"solution:\", z)\n    print(\"solution objective:\", tsp.tsp_value(z, adj_matrix))\n    draw_tsp_solution(tsp.graph, z, colors, pos)\n    \n    print(f\"Job ID is {result.job_id()}\")\n\nThe output I'm getting is given below.\n\n    (new_qiskit_env) PS F:\\XXXXX> & f:/XXXX/new_qiskit_env/Scripts/python.exe f:/XXXXXXX/tsp_qc_ibm\n    distance\n     [[ 0. 48. 91.]\n     [48.  0. 63.]\n     [91. 63.  0.]]\n    Problem name: TSP\n    \n    Minimize\n      48*x_0_0*x_1_1 + 48*x_0_0*x_1_2 + 91*x_0_0*x_2_1 + 91*x_0_0*x_2_2\n      + 48*x_0_1*x_1_0 + 48*x_0_1*x_1_2 + 91*x_0_1*x_2_0 + 91*x_0_1*x_2_2\n      + 48*x_0_2*x_1_0 + 48*x_0_2*x_1_1 + 91*x_0_2*x_2_0 + 91*x_0_2*x_2_1\n      + 63*x_1_0*x_2_1 + 63*x_1_0*x_2_2 + 63*x_1_1*x_2_0 + 63*x_1_1*x_2_2\n      + 63*x_1_2*x_2_0 + 63*x_1_2*x_2_1\n    \n    Subject to\n      Linear constraints (6)\n        x_0_0 + x_0_1 + x_0_2 == 1  'c0'\n        x_1_0 + x_1_1 + x_1_2 == 1  'c1'\n        x_2_0 + x_2_1 + x_2_2 == 1  'c2'\n        x_0_0 + x_1_0 + x_2_0 == 1  'c3'\n        x_0_1 + x_1_1 + x_2_1 == 1  'c4'\n        x_0_2 + x_1_2 + x_2_2 == 1  'c5'\n    \n      Binary variables (9)\n        x_0_0 x_0_1 x_0_2 x_1_0 x_1_1 x_1_2 x_2_0 x_2_1 x_2_2\n    \n    Offset: 7581.0\n    Ising Hamiltonian:\n    SparsePauliOp(['IIIIIIIIZ', 'IIIIIIIZI', 'IIIIIIZII', 'IIIIIZIII', 'IIIIZIIII', 'IIIZIIIII', 'IIZIIIIII', 'IZIIIIIII', 'ZIIIIIIII', 'IIIIIIIZZ', 'IIIIIIZIZ', 'IIIIIZIIZ', 'IIIIZIIIZ', 'IIIZIIIIZ', 'IIZIIIIIZ', 'IZIIIIIIZ', 'ZIIIIIIIZ', 'IIIIIIZZI', 'IIIIIZIZI', 'IIIIZIIZI', 'IIIZIIIZI', 'IIZIIIIZI', 'IZIIIIIZI', 'ZIIIIIIZI', 'IIIIIZZII', 'IIIIZIZII', 'IIIZIIZII', 'IIZIIIZII', 'IZIIIIZII', 'ZIIIIIZII', 'IIIIZZIII', 'IIIZIZIII', 'IIZIIZIII', 'IZIIIZIII', 'ZIIIIZIII', 'IIIZZIIII', 'IIZIZIIII', 'IZIIZIIII', 'ZIIIZIIII', 'IIZZIIIII', 'IZIZIIIII', 'ZIIZIIIII', 'IZZIIIIII', 'ZIZIIIIII', 'ZZIIIIIII'],      \n                  coeffs=[-1282.5 +0.j, -1282.5 +0.j, -1282.5 +0.j, -1268.5 +0.j, -1268.5 +0.j,\n     -1268.5 +0.j, -1290.  +0.j, -1290.  +0.j, -1290.  +0.j,   606.5 +0.j,\n       606.5 +0.j,   606.5 +0.j,    12.  +0.j,    12.  +0.j,   606.5 +0.j,\n        22.75+0.j,    22.75+0.j,   606.5 +0.j,    12.  +0.j,   606.5 +0.j,\n        12.  +0.j,    22.75+0.j,   606.5 +0.j,    22.75+0.j,    12.  +0.j,\n        12.  +0.j,   606.5 +0.j,    22.75+0.j,    22.75+0.j,   606.5 +0.j,\n       606.5 +0.j,   606.5 +0.j,   606.5 +0.j,    15.75+0.j,    15.75+0.j,\n       606.5 +0.j,    15.75+0.j,   606.5 +0.j,    15.75+0.j,    15.75+0.j,\n        15.75+0.j,   606.5 +0.j,   606.5 +0.j,   606.5 +0.j,   606.5 +0.j])\n    Traceback (most recent call last):\n      File \"f:\\XXXXX\\tsp_qc_ibm\", line 77, in <module>\n        qubit_circuit = qubitOp.to_circuit()\n                        ^^^^^^^^^^^^^^^^^^\n    AttributeError: 'SparsePauliOp' object has no attribute 'to_circuit'\n\nIf I dont use the to\\_circuit() and try to pass the problem directly to the sampler, the following error occurs.\n\n    result = sampler.run(qubitOp)\n\nError.\n\n        raise TypeError(\"circuit must be QuantumCircuit.\")\n    TypeError: circuit must be QuantumCircuit.",
  "label": "r/quantumcomputing",
  "dataType": "post",
  "communityName": "r/QuantumComputing",
  "datetime": "2024-04-12",
  "username_encoded": "Z0FBQUFBQm5LakwxQVpITWhYai1LMDdlWHYzVHNtd2V0VEtsZVY3NndJN1YyS0FJMzVMYTB0b3dXSlBkVm5xNDRGeTRBczh5VDFYWHdhSDV2ei1KUkN5dG1LVm1HSmhwMVE9PQ==",
  "url_encoded": "Z0FBQUFBQm5Lak9GY1dkYVJtUzBYV2x6WFV1YldMLWJvaDBBV1MtMWIyQklZZjRldjI2RXFEN2UyUS1XN1BFdE41OHNIcmtaZjFKSXVwLUJGa2JMRnBsWmJsMUJHa2x5TTM0RWhYRFQ2Z1lGQWR5MkdSUnZ4QnhMWC0tWGgxVHZGWG5fX2ozMXVkZkdwdl9nczJwd3dGTUVSNURMRnlUaWVQMTZnalBnYXprVGgwTnQ4Q09wZ2toR1FFZGlFSWQ0bDhrZktoX3hTY2RDVUd3TWhMMlpwTDI3MEwyMlpSbDgxUT09"
}

Entry Information