From eedb7f29b39c7c93c6fdf1c5c6bbc9c3655d96e4 Mon Sep 17 00:00:00 2001 From: "Davide Gessa (dakk)" Date: Mon, 30 Oct 2023 11:47:28 +0100 Subject: [PATCH] fix groover algorithm for python > 3.8 --- examples/groover_hash_collision.py | 4 ++++ examples/groover_hash_collision2.py | 12 +++++++++++- qlasskit/algorithms/groover.py | 12 ++++++++++-- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/examples/groover_hash_collision.py b/examples/groover_hash_collision.py index 647c5c0f..4b0779b8 100644 --- a/examples/groover_hash_collision.py +++ b/examples/groover_hash_collision.py @@ -10,6 +10,10 @@ def qiskit_simulate(qc): qc.measure_all() print(qc.draw("text")) + # from pyqrack import qrack_simulator + # from qiskit.providers.qrack import Qrack + # simulator = Qrack.get_backend("qasm_simulator") + simulator = Aer.get_backend("aer_simulator") circ = transpile(qc, simulator) result = simulator.run(circ).result() diff --git a/examples/groover_hash_collision2.py b/examples/groover_hash_collision2.py index ec9e5df3..cffe7941 100644 --- a/examples/groover_hash_collision2.py +++ b/examples/groover_hash_collision2.py @@ -14,8 +14,18 @@ def qiskit_simulate(qc, alog): qc.measure(algo.out_qubits(), c) print(qc.draw("text")) + simulator = Aer.get_backend("aer_simulator") - circ = transpile(qc, simulator) + circ = transpile (qc, simulator, optimization_level=3) + + # from pyqrack import qrack_simulator + # from qiskit.providers.qrack import Qrack + + # simulator = Qrack.get_backend("qasm_simulator") + # basis_gates = ["u", "cx"] + # circ = transpile(qc, simulator, basis_gates=basis_gates, optimization_level=3) + + result = simulator.run(circ).result() return result.get_counts(circ) diff --git a/qlasskit/algorithms/groover.py b/qlasskit/algorithms/groover.py index 67fbce29..1e6fffb4 100644 --- a/qlasskit/algorithms/groover.py +++ b/qlasskit/algorithms/groover.py @@ -13,6 +13,7 @@ # limitations under the License. import math +import sys from typing import List, Optional, Tuple, Union, get_args from ..qcircuit import QCircuit, gates @@ -22,7 +23,7 @@ class Groover(QAlgorithm): - def __init__( + def __init__( # noqa: C901 self, oracle: QlassF, element_to_search: Qtype, @@ -58,9 +59,16 @@ def __init__( if element_to_search is not None: if hasattr(self.oracle.args[0].ttype, "__name__"): argt_name = self.oracle.args[0].ttype.__name__ # type: ignore + + args = get_args(self.oracle.args[0].ttype) + if len(args) > 0: + argt_name += "[" + argt_name += ",".join([x.__name__ for x in args]) + argt_name += "]" + elif self.oracle.args[0].ttype == bool: argt_name = "bool" - else: + elif sys.version_info < (3, 8): argt_name = "Tuple[" argt_name += ",".join( [x.__name__ for x in get_args(self.oracle.args[0].ttype)]