Skip to content

Commit

Permalink
replace expr rather than modiyfing expr.var
Browse files Browse the repository at this point in the history
  • Loading branch information
hhorii committed Oct 11, 2023
1 parent 4fdaae6 commit 5c62d3f
Showing 1 changed file with 5 additions and 37 deletions.
42 changes: 5 additions & 37 deletions qiskit_aer/backends/aer_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,39 +215,6 @@ def _inline_circuit(self, circ, continue_label, break_label, bit_map=None):

return ret

class _ClbitConverter(ExprVisitor):
"""Apply cbit index conversion in Expr tree"""

def __init__(self, bit_map):
self.bit_map = bit_map

def visit_value(self, node, /):
# pylint: disable=unused-variable
return node

def visit_var(self, node, /):
if isinstance(node.var, Clbit):
node.var = self.bit_map[node.var]
else:
node.var = [self.bit_map[clbit] for clbit in node.var]
return node

def visit_cast(self, node, /):
node.operand.accept(self)
return node

def visit_unary(self, node, /):
node.operand.accept(self)
return node

def visit_binary(self, node, /):
node.left.accept(self)
node.right.accept(self)
return node

def visit_generic(self, node, /):
raise AerError(f"unsupported expression is used: {node.__class__}")

def _convert_jump_conditional(self, cond_tuple, bit_map):
"""Convert a condition tuple according to the wire map."""
if isinstance(cond_tuple, Expr):
Expand All @@ -258,11 +225,12 @@ def _convert_jump_conditional(self, cond_tuple, bit_map):
# ClassicalRegister conditions should already be in the outer circuit.
return cond_tuple
elif isinstance(cond_tuple[0], Var):
expr = deepcopy(cond_tuple[0])
if isinstance(expr.var, Clbit):
expr.var = bit_map[expr.var]
if isinstance(cond_tuple[0].var, Clbit):
expr = Var(bit_map[cond_tuple[0].var], cond_tuple[0].type)
elif isinstance(cond_tuple[0].var, ClassicalRegister):
expr = Var([bit_map[clbit] for clbit in cond_tuple[0].var], cond_tuple[0].type)
else:
expr.var = [bit_map[clbit] for clbit in expr.var]
raise AerError(f"jump condition does not support this tyep of Var: {cond_tuple[0]}.")
return (expr, cond_tuple[1])

raise AerError(f"jump condition does not support {cond_tuple[0].__class__}.")
Expand Down

0 comments on commit 5c62d3f

Please sign in to comment.