Skip to content

Commit

Permalink
✨ adjusted to Qiskit v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
nquetschlich committed Mar 15, 2024
1 parent 4f0b34b commit b4aa4b7
Show file tree
Hide file tree
Showing 21 changed files with 43 additions and 50 deletions.
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ requires-python = ">=3.10"
dynamic = ["version"]

dependencies = [
"pytket-qiskit>=0.48.0,<0.50.0", # manages the dependencies for qiskit and tket in a combined fashion
"pytket-qiskit>=0.49.0,<0.50.0", # manages the dependencies for qiskit and tket in a combined fashion
"qiskit_optimization",
"qiskit_nature",
"qiskit_finance",
Expand Down Expand Up @@ -119,7 +119,7 @@ module = ["pytket.*", "qiskit_finance.*"]
implicit_reexport = true

[[tool.mypy.overrides]]
module = ["qiskit.*", "qiskit_finance.*", "joblib.*", "networkx.*", "pandas.*"]
module = ["qiskit.*", "qiskit_finance.*", "joblib.*", "networkx.*", "pandas.*", "qiskit_algorithms.*", "qiskit_ibm_runtime.*"]
ignore_missing_imports = true

[tool.ruff]
Expand Down
2 changes: 1 addition & 1 deletion src/mqt/bench/benchmarks/ae.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import numpy as np
from qiskit import QuantumCircuit
from qiskit.algorithms import AmplitudeEstimation, EstimationProblem
from qiskit_algorithms import AmplitudeEstimation, EstimationProblem


def create_circuit(num_qubits: int) -> QuantumCircuit:
Expand Down
2 changes: 1 addition & 1 deletion src/mqt/bench/benchmarks/grover.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import numpy as np
from qiskit import AncillaRegister, QuantumCircuit, QuantumRegister
from qiskit.algorithms import Grover
from qiskit.circuit.library import GroverOperator
from qiskit_algorithms import Grover


def create_circuit(num_qubits: int, ancillary_mode: str = "noancilla") -> QuantumCircuit:
Expand Down
6 changes: 3 additions & 3 deletions src/mqt/bench/benchmarks/qaoa.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

from typing import TYPE_CHECKING

from qiskit.algorithms.minimum_eigensolvers import QAOA
from qiskit.algorithms.optimizers import SLSQP
from qiskit.primitives import Sampler
from qiskit_algorithms.minimum_eigensolvers import QAOA
from qiskit_algorithms.optimizers import SLSQP
from qiskit_optimization import QuadraticProgram

from mqt.bench.utils import get_examplary_max_cut_qp
Expand All @@ -28,7 +28,7 @@ def create_circuit(num_qubits: int) -> QuantumCircuit:

qaoa = QAOA(sampler=Sampler(), reps=2, optimizer=SLSQP(maxiter=25))
qaoa_result = qaoa.compute_minimum_eigenvalue(qp.to_ising()[0])
qc = qaoa.ansatz.bind_parameters(qaoa_result.optimal_point)
qc = qaoa.ansatz.assign_parameters(qaoa_result.optimal_point)

qc.name = "qaoa"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import datetime
from typing import TYPE_CHECKING

from qiskit.algorithms.minimum_eigensolvers import QAOA
from qiskit.algorithms.optimizers import COBYLA
from qiskit.primitives import Sampler
from qiskit_algorithms.minimum_eigensolvers import QAOA
from qiskit_algorithms.optimizers import COBYLA
from qiskit_finance.applications import PortfolioOptimization
from qiskit_finance.data_providers import RandomDataProvider
from qiskit_optimization.converters import QuadraticProgramToQubo
Expand Down Expand Up @@ -51,7 +51,7 @@ def create_circuit(num_qubits: int) -> QuantumCircuit:
qaoa = QAOA(sampler=Sampler(), optimizer=cobyla, reps=3)
qaoa.random_seed = 10
qaoa_result = qaoa.compute_minimum_eigenvalue(qp_qubo.to_ising()[0])
qc = qaoa.ansatz.bind_parameters(qaoa_result.optimal_point)
qc = qaoa.ansatz.assign_parameters(qaoa_result.optimal_point)

qc.name = "portfolioqaoa"
qc.measure_all()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import datetime
from typing import TYPE_CHECKING

from qiskit.algorithms.minimum_eigensolvers import VQE
from qiskit.algorithms.optimizers import COBYLA
from qiskit.circuit.library import TwoLocal
from qiskit.primitives import Estimator
from qiskit_algorithms.minimum_eigensolvers import VQE
from qiskit_algorithms.optimizers import COBYLA
from qiskit_finance.applications import PortfolioOptimization
from qiskit_finance.data_providers import RandomDataProvider
from qiskit_optimization.converters import QuadraticProgramToQubo
Expand Down Expand Up @@ -52,7 +52,7 @@ def create_circuit(num_qubits: int) -> QuantumCircuit:
vqe = VQE(ansatz=ry, optimizer=cobyla, estimator=Estimator())
vqe.random_seed = 10
vqe_result = vqe.compute_minimum_eigenvalue(qp_qubo.to_ising()[0])
qc = vqe.ansatz.bind_parameters(vqe_result.optimal_point)
qc = vqe.ansatz.assign_parameters(vqe_result.optimal_point)

qc.name = "portfoliovqe"
qc.measure_all()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from typing import TYPE_CHECKING

import numpy as np
from qiskit.algorithms import IterativeAmplitudeEstimation
from qiskit_algorithms import IterativeAmplitudeEstimation
from qiskit_finance.applications.estimation import EuropeanCallPricing
from qiskit_finance.circuit.library import LogNormalDistribution

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from typing import TYPE_CHECKING

import numpy as np
from qiskit.algorithms import EstimationProblem, IterativeAmplitudeEstimation
from qiskit.circuit.library import LinearAmplitudeFunction
from qiskit_algorithms import EstimationProblem, IterativeAmplitudeEstimation
from qiskit_finance.circuit.library import LogNormalDistribution

if TYPE_CHECKING: # pragma: no cover
Expand Down
4 changes: 2 additions & 2 deletions src/mqt/bench/benchmarks/qiskit_application_ml/qnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ def create_circuit(num_qubits: int) -> QuantumCircuit:
ansatz = RealAmplitudes(num_qubits=num_qubits, reps=1)

qc = QuantumCircuit(num_qubits)
feature_map = feature_map.bind_parameters([1 for _ in range(feature_map.num_parameters)])
ansatz = ansatz.bind_parameters(np.random.rand(ansatz.num_parameters))
feature_map = feature_map.assign_parameters([1 for _ in range(feature_map.num_parameters)])
ansatz = ansatz.assign_parameters(np.random.rand(ansatz.num_parameters))
qc.compose(feature_map, inplace=True)
qc.compose(ansatz, inplace=True)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

from typing import TYPE_CHECKING

from qiskit.algorithms.minimum_eigensolvers import VQE
from qiskit.algorithms.optimizers import COBYLA
from qiskit.circuit.library import TwoLocal
from qiskit.primitives import Estimator
from qiskit_algorithms.minimum_eigensolvers import VQE
from qiskit_algorithms.optimizers import COBYLA
from qiskit_nature.second_q.drivers import PySCFDriver
from qiskit_nature.second_q.mappers import JordanWignerMapper

Expand Down Expand Up @@ -41,7 +41,7 @@ def create_circuit(choice: str) -> QuantumCircuit:
another_solver = VQE(ansatz=tl_circuit, estimator=Estimator(), optimizer=COBYLA(maxiter=25))

result = another_solver.compute_minimum_eigenvalue(operator)
qc = another_solver.ansatz.bind_parameters(result.optimal_point)
qc = another_solver.ansatz.assign_parameters(result.optimal_point)

qc.name = "groundstate"
qc.name = qc.name + "_" + choice
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
from typing import TYPE_CHECKING, cast

import numpy as np
from qiskit.algorithms.minimum_eigensolvers import VQE
from qiskit.algorithms.optimizers import SLSQP
from qiskit.circuit.library import RealAmplitudes
from qiskit.primitives import Estimator
from qiskit.utils import algorithm_globals
from qiskit_algorithms.minimum_eigensolvers import VQE
from qiskit_algorithms.optimizers import SLSQP
from qiskit_optimization import QuadraticProgram
from qiskit_optimization.problems import LinearExpression, QuadraticExpression

Expand Down Expand Up @@ -120,12 +119,10 @@ def construct_problem(self, Q: QuadraticExpression, g: LinearExpression, c: floa
return qp

def solve_problem(self, qp: QuadraticProgram) -> QuantumCircuit:
algorithm_globals.random_seed = 10

ansatz = RealAmplitudes(self.n)
vqe = VQE(estimator=Estimator(), optimizer=SLSQP(maxiter=25), ansatz=ansatz)
vqe_result = vqe.compute_minimum_eigenvalue(qp.to_ising()[0])
return vqe.ansatz.bind_parameters(vqe_result.optimal_point)
return vqe.ansatz.assign_parameters(vqe_result.optimal_point)


def create_circuit(num_nodes: int = 3, num_vehs: int = 2) -> QuantumCircuit:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@

from typing import TYPE_CHECKING

from qiskit.algorithms.minimum_eigensolvers import VQE
from qiskit.algorithms.optimizers import SPSA
from qiskit.circuit.library import TwoLocal
from qiskit.primitives import Estimator
from qiskit.utils import algorithm_globals
from qiskit_algorithms.minimum_eigensolvers import VQE
from qiskit_algorithms.optimizers import SPSA
from qiskit_optimization.applications import Tsp
from qiskit_optimization.converters import QuadraticProgramToQubo

Expand All @@ -33,14 +32,12 @@ def create_circuit(num_nodes: int) -> QuantumCircuit:
qubo = qp2qubo.convert(qp)
qubit_op, offset = qubo.to_ising()

algorithm_globals.random_seed = 10

spsa = SPSA(maxiter=25)
ry = TwoLocal(qubit_op.num_qubits, "ry", "cz", reps=5, entanglement="linear")
vqe = VQE(ansatz=ry, optimizer=spsa, estimator=Estimator())

vqe_result = vqe.compute_minimum_eigenvalue(qubit_op)
qc = vqe.ansatz.bind_parameters(vqe_result.optimal_point)
qc = vqe.ansatz.assign_parameters(vqe_result.optimal_point)
qc.measure_all()
qc.name = "tsp"

Expand Down
2 changes: 1 addition & 1 deletion src/mqt/bench/benchmarks/realamprandom.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def create_circuit(num_qubits: int) -> QuantumCircuit:
np.random.seed(10)
qc = RealAmplitudes(num_qubits, entanglement="full", reps=3)
num_params = qc.num_parameters
qc = qc.bind_parameters(2 * np.pi * np.random.rand(num_params))
qc = qc.assign_parameters(2 * np.pi * np.random.rand(num_params))
qc.measure_all()
qc.name = "realamprandom"

Expand Down
3 changes: 0 additions & 3 deletions src/mqt/bench/benchmarks/shor.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
from qiskit import QuantumCircuit, QuantumRegister
from qiskit.circuit import Gate, Instruction, ParameterVector
from qiskit.circuit.library import QFT
from qiskit.utils.validation import validate_min


def create_circuit(num_to_be_factorized: int, a: int = 2) -> QuantumCircuit:
Expand Down Expand Up @@ -203,8 +202,6 @@ def _validate_input(N: int, a: int) -> None:
ValueError: Invalid input
"""
validate_min("N", N, 3)
validate_min("a", a, 2)

if N < 1 or N % 2 == 0:
msg = "The input needs to be an odd integer greater than 1."
Expand Down
2 changes: 1 addition & 1 deletion src/mqt/bench/benchmarks/su2random.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def create_circuit(num_qubits: int) -> QuantumCircuit:
np.random.seed(10)
qc = EfficientSU2(num_qubits, entanglement="full", reps=3)
num_params = qc.num_parameters
qc = qc.bind_parameters(2 * np.pi * np.random.rand(num_params))
qc = qc.assign_parameters(2 * np.pi * np.random.rand(num_params))
qc.measure_all()
qc.name = "su2random"

Expand Down
2 changes: 1 addition & 1 deletion src/mqt/bench/benchmarks/twolocalrandom.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def create_circuit(num_qubits: int) -> QuantumCircuit:
np.random.seed(10)
qc = TwoLocal(num_qubits, "ry", "cx", entanglement="full", reps=3)
num_params = qc.num_parameters
qc = qc.bind_parameters(2 * np.pi * np.random.rand(num_params))
qc = qc.assign_parameters(2 * np.pi * np.random.rand(num_params))
qc.measure_all()
qc.name = "twolocalrandom"

Expand Down
6 changes: 3 additions & 3 deletions src/mqt/bench/benchmarks/vqe.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

from typing import TYPE_CHECKING

from qiskit.algorithms.minimum_eigensolvers import VQE
from qiskit.algorithms.optimizers import SLSQP
from qiskit.circuit.library import RealAmplitudes
from qiskit.primitives import Estimator
from qiskit_algorithms.minimum_eigensolvers import VQE
from qiskit_algorithms.optimizers import SLSQP
from qiskit_optimization import QuadraticProgram

from mqt.bench.utils import get_examplary_max_cut_qp
Expand All @@ -30,7 +30,7 @@ def create_circuit(num_qubits: int) -> QuantumCircuit:
ansatz = RealAmplitudes(num_qubits, reps=2)
vqe = VQE(ansatz=ansatz, optimizer=SLSQP(maxiter=25), estimator=Estimator())
vqe_result = vqe.compute_minimum_eigenvalue(qp.to_ising()[0])
qc = vqe.ansatz.bind_parameters(vqe_result.optimal_point)
qc = vqe.ansatz.assign_parameters(vqe_result.optimal_point)

qc.measure_all()
qc.name = "vqe"
Expand Down
9 changes: 5 additions & 4 deletions src/mqt/bench/qiskit_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

from mqt.bench.devices import Device, Provider

from qiskit.compiler import transpile
from qiskit import transpile
from qiskit.qasm2 import dumps

from mqt.bench import utils

Expand Down Expand Up @@ -69,7 +70,7 @@ def get_indep_level(
if return_qc:
return target_independent
return utils.save_as_qasm(
target_independent.qasm(),
dumps(target_independent),
filename_indep,
target_directory=target_directory,
)
Expand Down Expand Up @@ -146,7 +147,7 @@ def get_native_gates_level(
if return_qc:
return compiled_without_architecture
return utils.save_as_qasm(
compiled_without_architecture.qasm(),
dumps(compiled_without_architecture),
filename_native,
gate_set,
target_directory=target_directory,
Expand Down Expand Up @@ -226,7 +227,7 @@ def get_mapped_level(
if return_qc:
return compiled_with_architecture
return utils.save_as_qasm(
compiled_with_architecture.qasm(),
dumps(compiled_with_architecture),
filename_mapped,
device.basis_gates,
True,
Expand Down
5 changes: 2 additions & 3 deletions src/mqt/bench/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import networkx as nx
import numpy as np
from pytket import __version__ as __tket_version__
from qiskit import QuantumCircuit, __qiskit_version__
from qiskit import QuantumCircuit, __version__
from qiskit.converters import circuit_to_dag
from qiskit_optimization.applications import Maxcut

Expand Down Expand Up @@ -195,10 +195,9 @@ def save_as_qasm(
f.write("// For more information about MQT Bench, please visit https://www.cda.cit.tum.de/mqtbench/\n")
f.write("// MQT Bench version: " + mqtbench_module_version + "\n")
if "qiskit" in filename:
f.write("// Qiskit version: " + str(__qiskit_version__) + "\n")
f.write("// Qiskit version: " + str(__version__) + "\n")
elif "tket" in filename:
f.write("// TKET version: " + str(__tket_version__) + "\n")

if gate_set:
f.write("// Used Gate Set: " + str(gate_set) + "\n")
if mapped:
Expand Down
2 changes: 1 addition & 1 deletion tests/devices/test_ibm_device_support.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

import pytest
from qiskit.providers.fake_provider import FakeMontreal, FakeMontrealV2
from qiskit_ibm_runtime.fake_provider import FakeMontreal, FakeMontrealV2

from mqt.bench.devices import IBMProvider

Expand Down
4 changes: 3 additions & 1 deletion tests/test_bench.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from typing import TYPE_CHECKING

import pytket
from qiskit.qasm2 import dump

if TYPE_CHECKING: # pragma: no cover
import types
Expand Down Expand Up @@ -1013,7 +1014,8 @@ def test_oqc_postprocessing() -> None:
def test_evaluate_qasm_file() -> None:
qc = get_benchmark("dj", 1, 5)
filename = "test_5.qasm"
qc.qasm(filename=filename)
with Path(filename).open("w") as f:
dump(qc, f)
path = Path(filename)
res = evaluation.evaluate_qasm_file(filename)
assert type(res) == evaluation.EvaluationResult
Expand Down

0 comments on commit b4aa4b7

Please sign in to comment.