Skip to content

Commit

Permalink
refactor: made _get_gate and _unroll_expr more readable
Browse files Browse the repository at this point in the history
  • Loading branch information
BrunoLiegiBastonLiegi committed Feb 26, 2024
1 parent bb0da07 commit 7f3728b
Showing 1 changed file with 34 additions and 22 deletions.
56 changes: 34 additions & 22 deletions src/qibo/_openqasm.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,42 +158,54 @@ def _get_gate(self, gate):
pass
init_args.append(arg)
# check whether the gate exists in qibo.gates already
try:
gates = [
getattr(qibo.gates, _qibo_gate_name(gate.name.name))(
*qubits, *init_args
if _qibo_gate_name(gate.name.name) in dir(qibo.gates):
try:
gates = [
getattr(qibo.gates, _qibo_gate_name(gate.name.name))(
*qubits, *init_args
)
]
# the gate exists in qibo.gates but invalid construction
except TypeError:
raise_error(
ValueError, f"Invalid gate declaration at span: {gate.span}"
)
]
except:
# check whether the gate was defined by the user
elif gate.name.name in self.defined_gates:
try:
# check whether the gate was defined by the user
gates = self.defined_gates.get(gate.name.name).get_gates(
qubits, init_args
)
except:
# the gate exists in self.defined_gates but invalid construction
except TypeError:
raise_error(
ValueError, f"Invalid gate declaration at span: {gate.span}"
)
# undefined gate
else:
raise_error(ValueError, f"Undefined gate at span: {gate.span}")
return gates

def _unroll_expression(self, expr):
"""Unrolls an argument definition expression to retrieve the
complete argument as a string."""
try:
# check whether the expression is a simple string, e.g. `pi` or `theta`
if "name" in dir(expr):
return expr.name
except:
try:
return expr.value
except:
expr_dict = {}
for attr in ("lhs", "op", "expression", "rhs"):
expr_dict[attr] = ""
try:
val = self._unroll_expression(getattr(expr, attr))
except:
continue
expr_dict[attr] += str(val)
return "".join(list(expr_dict.values()))
# check whether the expression is a single value, e.g. `0.1234`
elif "value" in dir(expr):
return expr.value
# the expression is composite, e.g. `2*pi` or `3*theta/2`
else:
expr_dict = {}
for attr in ("lhs", "op", "expression", "rhs"):
expr_dict[attr] = ""
if attr in dir(expr):
val = self._unroll_expression(getattr(expr, attr))
else:
continue
expr_dict[attr] += str(val)
return "".join(list(expr_dict.values()))

def _def_gate(self, definition):
"""Converts a :class:`openqasm3.ast.QuantumGateDefinition` statement
Expand Down

0 comments on commit 7f3728b

Please sign in to comment.