Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/pyqasm/maps/gates.py
Original file line number Diff line number Diff line change
Expand Up @@ -1167,3 +1167,48 @@ def map_qasm_inv_op_to_callable(op_name: str):
InversionOp.INVERT_ROTATION,
)
raise ValidationError(f"Unsupported / undeclared QASM operation: {op_name}")
Comment thread
TheGupta2012 marked this conversation as resolved.


CTRL_GATE_MAP = {
"x": "cx",
"y": "cy",
"z": "cz",
"rx": "crx",
"ry": "cry",
"rz": "crz",
"p": "cp",
"h": "ch",
"u": "cu",
"swap": "cswap",
"cx": "ccx",
}


def map_qasm_ctrl_op_to_callable(op_name: str, ctrl_count: int):
"""
Map a controlled QASM operation to a callable.

Args:
op_name (str): The QASM operation name.
ctrl_count (int): The number of control qubits.

Returns:
tuple: A tuple containing the callable and the number of qubits the operation acts on.
"""

ctrl_op_name, c = op_name, ctrl_count
while c > 0 and ctrl_op_name in CTRL_GATE_MAP:
ctrl_op_name = CTRL_GATE_MAP[ctrl_op_name]
c -= 1
if c == 0:
if ctrl_op_name in ONE_QUBIT_OP_MAP:
return ONE_QUBIT_OP_MAP[ctrl_op_name], 1
if ctrl_op_name in TWO_QUBIT_OP_MAP:
return TWO_QUBIT_OP_MAP[ctrl_op_name], 2
if ctrl_op_name in THREE_QUBIT_OP_MAP:
return THREE_QUBIT_OP_MAP[ctrl_op_name], 3

# TODO: decompose controls if not built in
raise ValidationError(
f"Unsupported controlled QASM operation: {op_name} with {ctrl_count} controls"
)
Loading