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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ ValidationError: Expected 1 parameter for gate 'rx', but got 2
### Deprecated

### Removed
- Removed the dependency on `Union` for typing by replacing it with `|` ([#170](https://github.com/qBraid/pyqasm/pull/170)).

### Fixed
- Resolved the inconsistency in `pyqasm.printer.draw` and `pyqasm.printer.mpl_draw` behaviour for multiple function calls. See issue [#165](https://github.com/qBraid/pyqasm/issues/165) for bug details. ([#168](https://github.com/qBraid/pyqasm/pull/168))
Expand Down
4 changes: 2 additions & 2 deletions src/pyqasm/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from __future__ import annotations

import re
from typing import TYPE_CHECKING, Any, Optional, Union
from typing import TYPE_CHECKING, Any, Optional

import numpy as np
from openqasm3.ast import (
Expand Down Expand Up @@ -144,7 +144,7 @@ def _validate_step(start_id, end_id, step, span):
@staticmethod
def analyze_index_expression(
index_expr: IndexExpression,
) -> tuple[str, list[Union[Any, Expression, RangeDefinition]]]:
) -> tuple[str, list[Any | Expression | RangeDefinition]]:
"""Analyze an index expression to get the variable name and indices.

Args:
Expand Down
6 changes: 3 additions & 3 deletions src/pyqasm/elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"""
from dataclasses import dataclass
from enum import Enum
from typing import Any, Optional, Union
from typing import Any, Optional

import numpy as np

Expand Down Expand Up @@ -88,7 +88,7 @@ class Variable: # pylint: disable=too-many-instance-attributes
base_type (Any): Base type of the variable.
base_size (int): Base size of the variable.
dims (Optional[List[int]]): Dimensions of the variable.
value (Optional[Union[int, float, np.ndarray]]): Value of the variable.
value (Optional[int | float | np.ndarray]): Value of the variable.
is_constant (bool): Flag indicating if the variable is constant.
is_register (bool): Flag indicating if the variable is a register.
readonly (bool): Flag indicating if the variable is readonly.
Expand All @@ -98,7 +98,7 @@ class Variable: # pylint: disable=too-many-instance-attributes
base_type: Any
base_size: int
dims: Optional[list[int]] = None
value: Optional[Union[int, float, np.ndarray]] = None
value: Optional[int | float | np.ndarray] = None
is_constant: bool = False
is_register: bool = False
readonly: bool = False
Expand Down
16 changes: 8 additions & 8 deletions src/pyqasm/maps/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@

"""

from typing import Callable, Union
from typing import Callable

import numpy as np
from openqasm3.ast import AngleType, BitType, BoolType, ComplexType, FloatType, IntType, UintType

from pyqasm.exceptions import ValidationError

# Define the type for the operator functions
OperatorFunction = Union[
Callable[[Union[int, float, bool]], Union[int, float, bool]],
Callable[[Union[int, float, bool], Union[int, float, bool]], Union[int, float, bool]],
]
OperatorFunction = (
Callable[[int | float | bool], int | float | bool]
| Callable[[int | float | bool, int | float | bool], int | float | bool]
)


OPERATOR_MAP: dict[str, OperatorFunction] = {
Expand Down Expand Up @@ -56,18 +56,18 @@
}


def qasm3_expression_op_map(op_name: str, *args) -> Union[float, int, bool]:
def qasm3_expression_op_map(op_name: str, *args) -> float | int | bool:
"""
Return the result of applying the given operator to the given operands.

Args:
op_name (str): The operator name.
*args: The operands of type Union[int, float, bool]
*args: The operands of type int | float | bool
1. For unary operators, a single operand (e.g., ~3)
2. For binary operators, two operands (e.g., 3 + 2)

Returns:
(Union[float, int, bool]): The result of applying the operator to the operands.
(float | int | bool): The result of applying the operator to the operands.
"""
try:
operator = OPERATOR_MAP[op_name]
Expand Down
76 changes: 38 additions & 38 deletions src/pyqasm/maps/gates.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"""


from typing import Callable, Union
from typing import Callable

import numpy as np
from openqasm3.ast import FloatLiteral, Identifier, IndexedIdentifier, QuantumGate, QuantumPhase
Expand All @@ -32,9 +32,9 @@


def u3_gate(
theta: Union[int, float],
phi: Union[int, float],
lam: Union[int, float],
theta: int | float,
phi: int | float,
lam: int | float,
qubit_id,
) -> list[QuantumGate]:
"""
Expand All @@ -44,9 +44,9 @@ def u3_gate(

Args:
name (str): The name of the gate.
theta (Union[int, float]): The theta parameter.
phi (Union[int, float]): The phi parameter.
lam (Union[int, float]): The lambda parameter.
theta (int | float): The theta parameter.
phi (int | float): The phi parameter.
lam (int | float): The lambda parameter.
qubit_id (IndexedIdentifier): The qubit on which to apply the gate.

Returns:
Expand All @@ -63,9 +63,9 @@ def u3_gate(


def u3_inv_gate(
theta: Union[int, float],
phi: Union[int, float],
lam: Union[int, float],
theta: int | float,
phi: int | float,
lam: int | float,
qubits,
) -> list[QuantumGate]:
"""
Expand Down Expand Up @@ -165,7 +165,7 @@ def ch_gate(qubit0: IndexedIdentifier, qubit1: IndexedIdentifier) -> list[Quantu


def xy_gate(
theta: Union[int, float], qubit0: IndexedIdentifier, qubit1: IndexedIdentifier
theta: int | float, qubit0: IndexedIdentifier, qubit1: IndexedIdentifier
) -> list[QuantumGate]:
"""Implements the XXPlusYY gate matrix as defined by braket.

Expand All @@ -177,8 +177,8 @@ def xy_gate(


def xx_plus_yy_gate(
theta: Union[int, float],
phi: Union[int, float],
theta: int | float,
phi: int | float,
qubit0: IndexedIdentifier,
qubit1: IndexedIdentifier,
) -> list[QuantumGate]:
Expand Down Expand Up @@ -224,7 +224,7 @@ def xx_plus_yy_gate(


def ryy_gate(
theta: Union[int, float], qubit0: IndexedIdentifier, qubit1: IndexedIdentifier
theta: int | float, qubit0: IndexedIdentifier, qubit1: IndexedIdentifier
) -> list[QuantumGate]:
"""
Implements the YY gate as a decomposition of other gates.
Expand Down Expand Up @@ -260,7 +260,7 @@ def ryy_gate(


def zz_gate(
theta: Union[int, float], qubit0: IndexedIdentifier, qubit1: IndexedIdentifier
theta: int | float, qubit0: IndexedIdentifier, qubit1: IndexedIdentifier
) -> list[QuantumGate]:
"""
Implements the ZZ gate as a decomposition of other gates.
Expand All @@ -274,7 +274,7 @@ def zz_gate(
return result


def phaseshift_gate(theta: Union[int, float], qubit: IndexedIdentifier) -> list[QuantumGate]:
def phaseshift_gate(theta: int | float, qubit: IndexedIdentifier) -> list[QuantumGate]:
"""
Implements the phase shift gate as a decomposition of other gates.
"""
Expand Down Expand Up @@ -313,7 +313,7 @@ def cswap_gate(


def pswap_gate(
theta: Union[int, float], qubit0: IndexedIdentifier, qubit1: IndexedIdentifier
theta: int | float, qubit0: IndexedIdentifier, qubit1: IndexedIdentifier
) -> list[QuantumGate]:
"""
Implements the PSWAP gate as a decomposition of other gates.
Expand Down Expand Up @@ -345,7 +345,7 @@ def iswap_gate(qubit0: IndexedIdentifier, qubit1: IndexedIdentifier) -> list[Qua


def crx_gate(
theta: Union[int, float], qubit0: IndexedIdentifier, qubit1: IndexedIdentifier
theta: int | float, qubit0: IndexedIdentifier, qubit1: IndexedIdentifier
) -> list[QuantumGate]:
"""
Implements the CRX gate as a decomposition of other gates.
Expand Down Expand Up @@ -378,7 +378,7 @@ def crx_gate(


def cry_gate(
theta: Union[int, float], qubit0: IndexedIdentifier, qubit1: IndexedIdentifier
theta: int | float, qubit0: IndexedIdentifier, qubit1: IndexedIdentifier
) -> list[QuantumGate]:
"""
Implements the CRY gate as a decomposition of other gates.
Expand Down Expand Up @@ -410,7 +410,7 @@ def cry_gate(


def crz_gate(
theta: Union[int, float], qubit0: IndexedIdentifier, qubit1: IndexedIdentifier
theta: int | float, qubit0: IndexedIdentifier, qubit1: IndexedIdentifier
) -> list[QuantumGate]:
"""
Implements the CRZ gate as a decomposition of other gates.
Expand Down Expand Up @@ -443,10 +443,10 @@ def crz_gate(


def cu_gate( # pylint: disable=too-many-arguments
theta: Union[int, float],
phi: Union[int, float],
lam: Union[int, float],
gamma: Union[int, float],
theta: int | float,
phi: int | float,
lam: int | float,
gamma: int | float,
qubit0: IndexedIdentifier,
qubit1: IndexedIdentifier,
) -> list[QuantumGate]:
Expand Down Expand Up @@ -489,9 +489,9 @@ def cu_gate( # pylint: disable=too-many-arguments


def cu3_gate( # pylint: disable=too-many-arguments
theta: Union[int, float],
phi: Union[int, float],
lam: Union[int, float],
theta: int | float,
phi: int | float,
lam: int | float,
qubit0: IndexedIdentifier,
qubit1: IndexedIdentifier,
) -> list[QuantumGate]:
Expand Down Expand Up @@ -528,7 +528,7 @@ def cu3_gate( # pylint: disable=too-many-arguments


def cu1_gate(
theta: Union[int, float], qubit0: IndexedIdentifier, qubit1: IndexedIdentifier
theta: int | float, qubit0: IndexedIdentifier, qubit1: IndexedIdentifier
) -> list[QuantumGate]:
"""
Implements the CU1 gate as a decomposition of other gates.
Expand Down Expand Up @@ -600,13 +600,13 @@ def csx_gate(qubit0: IndexedIdentifier, qubit1: IndexedIdentifier) -> list[Quant


def rxx_gate(
theta: Union[int, float], qubit0: IndexedIdentifier, qubit1: IndexedIdentifier
) -> list[Union[QuantumGate, QuantumPhase]]:
theta: int | float, qubit0: IndexedIdentifier, qubit1: IndexedIdentifier
) -> list[QuantumGate | QuantumPhase]:
"""
Implements the RXX gate as a decomposition of other gates.
"""

result: list[Union[QuantumGate, QuantumPhase]] = []
result: list[QuantumGate | QuantumPhase] = []
result.extend(global_phase_gate(-theta / 2, [qubit0, qubit1]))
result.extend(one_qubit_gate_op("h", qubit0))
result.extend(one_qubit_gate_op("h", qubit1))
Expand Down Expand Up @@ -637,8 +637,8 @@ def rccx_gate(


def rzz_gate(
theta: Union[int, float], qubit0: IndexedIdentifier, qubit1: IndexedIdentifier
) -> list[Union[QuantumGate, QuantumPhase]]:
theta: int | float, qubit0: IndexedIdentifier, qubit1: IndexedIdentifier
) -> list[QuantumGate | QuantumPhase]:
"""
Implements the RZZ gate as a decomposition of other gates.

Expand All @@ -661,7 +661,7 @@ def rzz_gate(
q_1: ┤ X ├┤ U3(0,0,theta) ├┤ X ├
└───┘└───────────────┘└───┘
"""
result: list[Union[QuantumGate, QuantumPhase]] = []
result: list[QuantumGate | QuantumPhase] = []

result.extend(global_phase_gate(-theta / 2, [qubit0, qubit1]))
result.extend(two_qubit_gate_op("cx", qubit0, qubit1))
Expand All @@ -672,7 +672,7 @@ def rzz_gate(


def cphaseshift_gate(
theta: Union[int, float], qubit0: IndexedIdentifier, qubit1: IndexedIdentifier
theta: int | float, qubit0: IndexedIdentifier, qubit1: IndexedIdentifier
) -> list[QuantumGate]:
"""
Implements the controlled phase shift gate as a decomposition of other gates.
Expand Down Expand Up @@ -706,7 +706,7 @@ def cphaseshift_gate(


def cphaseshift00_gate(
theta: Union[int, float], qubit0: IndexedIdentifier, qubit1: IndexedIdentifier
theta: int | float, qubit0: IndexedIdentifier, qubit1: IndexedIdentifier
) -> list[QuantumGate]:
"""
Implements the controlled phase shift 00 gate as a decomposition of other gates.
Expand All @@ -725,7 +725,7 @@ def cphaseshift00_gate(


def cphaseshift01_gate(
theta: Union[int, float], qubit0: IndexedIdentifier, qubit1: IndexedIdentifier
theta: int | float, qubit0: IndexedIdentifier, qubit1: IndexedIdentifier
) -> list[QuantumGate]:
"""
Implements the controlled phase shift 01 gate as a decomposition of other gates.
Expand All @@ -742,7 +742,7 @@ def cphaseshift01_gate(


def cphaseshift10_gate(
theta: Union[int, float], qubit0: IndexedIdentifier, qubit1: IndexedIdentifier
theta: int | float, qubit0: IndexedIdentifier, qubit1: IndexedIdentifier
) -> list[QuantumGate]:
"""
Implements the controlled phase shift 10 gate as a decomposition of other gates.
Expand Down
5 changes: 2 additions & 3 deletions src/pyqasm/modules/qasm2.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import re
from copy import deepcopy
from typing import Union

import openqasm3.ast as qasm3_ast
from openqasm3.ast import Include, Program
Expand Down Expand Up @@ -78,7 +77,7 @@ def _qasm_ast_to_str(self, qasm_ast):
raw_qasm = dumps(qasm_ast, old_measurement=True)
return self._format_declarations(raw_qasm)

def to_qasm3(self, as_str: bool = False) -> Union[str, Qasm3Module]:
def to_qasm3(self, as_str: bool = False) -> str | Qasm3Module:
"""Convert the module to openqasm3 format

Args:
Expand All @@ -87,7 +86,7 @@ def to_qasm3(self, as_str: bool = False) -> Union[str, Qasm3Module]:
Default is False.

Returns:
Union[str, Qasm3Module]: The module in openqasm3 format.
str | Qasm3Module: The module in openqasm3 format.
"""
qasm_program = deepcopy(self._original_program)
# replace the include with stdgates.inc
Expand Down
6 changes: 3 additions & 3 deletions src/pyqasm/subroutines.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
Module containing the class for validating QASM3 subroutines.

"""
from typing import Optional, Union
from typing import Optional

from openqasm3.ast import (
AccessControl,
Expand Down Expand Up @@ -50,11 +50,11 @@ def set_visitor_obj(cls, visitor_obj) -> None:
cls.visitor_obj = visitor_obj

@staticmethod
def get_fn_actual_arg_name(actual_arg: Union[Identifier, IndexExpression]) -> Optional[str]:
def get_fn_actual_arg_name(actual_arg: Identifier | IndexExpression) -> Optional[str]:
"""Get the name of the actual argument passed to a function.

Args:
actual_arg (Union[Identifier, IndexExpression]): The actual argument passed to the
actual_arg (Identifier | IndexExpression): The actual argument passed to the
function.

Returns:
Expand Down
Loading