Skip to content

Commit

Permalink
fix groover algorithm for python > 3.8
Browse files Browse the repository at this point in the history
  • Loading branch information
dakk committed Oct 30, 2023
1 parent aba9a0e commit eedb7f2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
4 changes: 4 additions & 0 deletions examples/groover_hash_collision.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
12 changes: 11 additions & 1 deletion examples/groover_hash_collision2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 10 additions & 2 deletions qlasskit/algorithms/groover.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,7 +23,7 @@


class Groover(QAlgorithm):
def __init__(
def __init__( # noqa: C901
self,
oracle: QlassF,
element_to_search: Qtype,
Expand Down Expand Up @@ -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)]
Expand Down

0 comments on commit eedb7f2

Please sign in to comment.