Skip to content

Commit

Permalink
[py][nfc] imports clean up.
Browse files Browse the repository at this point in the history
Cleans up imports throughout the python runtime. I trie following
the principles in PEP8 (https://peps.python.org/pep-0008/#imports).

The changes include:
  * Grouping imports two blocks: external and internal, w.r.t cudaq.
  * Not using relative paths when the module is in a parent dir (`..`)
  * Not using wildcard imports
  * Removing unused ones

Signed-off-by: boschmitt <[email protected]>
  • Loading branch information
boschmitt committed Feb 5, 2025
1 parent a304c4f commit 77ba55a
Show file tree
Hide file tree
Showing 25 changed files with 161 additions and 124 deletions.
3 changes: 1 addition & 2 deletions python/cudaq/handlers/photonics_kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
# ============================================================================ #

from __future__ import annotations

from dataclasses import dataclass
from typing import List

from ..mlir._mlir_libs._quakeDialects import cudaq_runtime
from cudaq.mlir._mlir_libs._quakeDialects import cudaq_runtime


@dataclass
Expand Down
11 changes: 7 additions & 4 deletions python/cudaq/kernel/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
# the terms of the Apache License 2.0 which accompanies this distribution. #
# ============================================================================ #

import ast, inspect, importlib, textwrap
import ast
import inspect
import importlib
import textwrap

from cudaq.mlir._mlir_libs._quakeDialects import cudaq_runtime
from cudaq.mlir.dialects import cc
from .utils import globalAstRegistry, globalKernelRegistry, mlirTypeFromAnnotation
from ..mlir.dialects import cc
from ..mlir.ir import *
from ..mlir._mlir_libs._quakeDialects import cudaq_runtime


class MidCircuitMeasurementAnalyzer(ast.NodeVisitor):
Expand Down
36 changes: 26 additions & 10 deletions python/cudaq/kernel/ast_bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,36 @@
# the terms of the Apache License 2.0 which accompanies this distribution. #
# ============================================================================ #
import ast
import hashlib
import graphlib
import sys, os
from typing import Callable
from collections import deque
import numpy as np
import os
import sys
from collections import deque

from cudaq.mlir._mlir_libs._quakeDialects import (
cudaq_runtime, load_intrinsic, gen_vector_of_complex_constant,
register_all_dialects)
from cudaq.mlir.dialects import arith, cc, complex, func, math, quake
from cudaq.mlir.ir import (BoolAttr, Block, BlockArgument, Context, ComplexType,
DenseI32ArrayAttr, DenseI64ArrayAttr, DictAttr,
F32Type, F64Type, FlatSymbolRefAttr, FloatAttr,
FunctionType, InsertionPoint, IntegerAttr,
IntegerType, Location, Module, StringAttr,
SymbolTable, TypeAttr, UnitAttr)
from cudaq.mlir.passmanager import PassManager
from .analysis import FindDepKernelsVisitor
from .utils import globalAstRegistry, globalKernelRegistry, globalRegisteredOperations, nvqppPrefix, mlirTypeFromAnnotation, mlirTypeFromPyType, Color, mlirTypeToPyType, globalRegisteredTypes
from ..mlir.ir import *
from ..mlir.passmanager import *
from ..mlir.dialects import quake, cc
from ..mlir.dialects import builtin, func, arith, math, complex
from ..mlir._mlir_libs._quakeDialects import cudaq_runtime, load_intrinsic, register_all_dialects, gen_vector_of_complex_constant
from .captured_data import CapturedDataStorage
from .utils import (
Color,
globalAstRegistry,
globalKernelRegistry,
globalRegisteredOperations,
globalRegisteredTypes,
nvqppPrefix,
mlirTypeFromAnnotation,
mlirTypeFromPyType,
mlirTypeToPyType,
)

State = cudaq_runtime.State

Expand Down
17 changes: 7 additions & 10 deletions python/cudaq/kernel/captured_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@
import uuid
import numpy as np

from ..mlir.ir import *
from ..mlir.passmanager import *
from ..mlir.execution_engine import *
from ..mlir.dialects import quake, cc
from ..mlir.dialects import builtin, func, arith
from ..mlir._mlir_libs._quakeDialects import cudaq_runtime
from ..mlir._mlir_libs._quakeDialects.cudaq_runtime import deletePointersToCudaqState
from ..mlir._mlir_libs._quakeDialects.cudaq_runtime import deletePointersToStateData
from cudaq.mlir._mlir_libs._quakeDialects import cudaq_runtime
from cudaq.mlir.dialects import arith, cc, func
from cudaq.mlir.ir import (ComplexType, F32Type, F64Type, FlatSymbolRefAttr,
FunctionType, InsertionPoint, IntegerAttr,
IntegerType, StringAttr, TypeAttr)

kDynamicPtrIndex: int = -2147483648

Expand Down Expand Up @@ -48,8 +45,8 @@ def __del__(self):
"""
Remove pointers to stored data for the current kernel.
"""
deletePointersToCudaqState(self.cudaqStateIDs)
deletePointersToStateData(self.arrayIDs)
cudaq_runtime.deletePointersToCudaqState(self.cudaqStateIDs)
cudaq_runtime.deletePointersToStateData(self.arrayIDs)

def getIntegerAttr(self, type, value):
"""
Expand Down
55 changes: 38 additions & 17 deletions python/cudaq/kernel/kernel_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,49 @@
# the terms of the Apache License 2.0 which accompanies this distribution. #
# ============================================================================ #

from functools import partialmethod
import hashlib
import uuid
import numpy as np
import random
import re
import string
import sys
import numpy as np
from functools import partialmethod
from typing import get_origin
from .quake_value import QuakeValue
from .kernel_decorator import PyKernelDecorator
from .utils import mlirTypeFromPyType, nvqppPrefix, emitFatalError, emitWarning, mlirTypeToPyType, emitErrorIfInvalidPauli, globalRegisteredOperations
from .common.givens import givens_builder
from .common.fermionic_swap import fermionic_swap_builder
from .captured_data import CapturedDataStorage

from ..mlir.ir import *
from ..mlir.passmanager import *
from ..mlir.execution_engine import *
from ..mlir.dialects import quake, cc
from ..mlir.dialects import builtin, func, arith, math, complex as complexDialect
from ..mlir._mlir_libs._quakeDialects import cudaq_runtime, load_intrinsic, register_all_dialects, gen_vector_of_complex_constant
from cudaq.mlir.ir import (
BoolAttr,
Block,
ComplexType,
Context,
DenseI32ArrayAttr,
DictAttr,
F32Type,
F64Type,
FlatSymbolRefAttr,
FloatAttr,
InsertionPoint,
IntegerAttr,
IntegerType,
Location,
Module,
StringAttr,
SymbolTable,
TypeAttr,
UnitAttr,
)
from cudaq.mlir.passmanager import PassManager
from cudaq.mlir.execution_engine import ExecutionEngine
from cudaq.mlir.dialects import (complex as complexDialect, arith, quake, cc,
func, math)
from cudaq.mlir._mlir_libs._quakeDialects import (
cudaq_runtime, gen_vector_of_complex_constant, load_intrinsic,
register_all_dialects)
from .captured_data import CapturedDataStorage
from .common.fermionic_swap import fermionic_swap_builder
from .common.givens import givens_builder
from .kernel_decorator import PyKernelDecorator
from .quake_value import QuakeValue
from .utils import (emitErrorIfInvalidPauli, emitFatalError, emitWarning,
globalRegisteredOperations, mlirTypeFromPyType,
mlirTypeToPyType, nvqppPrefix)

kDynamicPtrIndex: int = -2147483648

Expand Down
23 changes: 12 additions & 11 deletions python/cudaq/kernel/kernel_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@
# This source code and the accompanying materials are made available under #
# the terms of the Apache License 2.0 which accompanies this distribution. #
# ============================================================================ #
import ast, sys, traceback
import ast
import importlib
import inspect
import json
from typing import Callable
from ..mlir.ir import *
from ..mlir.passmanager import *
from ..mlir.dialects import quake, cc, func
from .ast_bridge import compile_to_mlir, PyASTBridge
from .utils import mlirTypeFromPyType, nvqppPrefix, mlirTypeToPyType, globalAstRegistry, emitFatalError, emitErrorIfInvalidPauli, globalRegisteredTypes
import numpy as np

from cudaq.handlers import PhotonicsHandler
from cudaq.mlir._mlir_libs._quakeDialects import cudaq_runtime
from cudaq.mlir.dialects import cc, func
from cudaq.mlir.ir import (ComplexType, F32Type, F64Type, IntegerType,
SymbolTable)
from .analysis import MidCircuitMeasurementAnalyzer, HasReturnNodeVisitor
from ..mlir._mlir_libs._quakeDialects import cudaq_runtime
from .ast_bridge import compile_to_mlir, PyASTBridge
from .captured_data import CapturedDataStorage
from ..handlers import PhotonicsHandler

import numpy as np
from .utils import (emitFatalError, emitErrorIfInvalidPauli, globalAstRegistry,
globalRegisteredTypes, mlirTypeFromPyType, mlirTypeToPyType,
nvqppPrefix)

# This file implements the decorator mechanism needed to
# JIT compile CUDA-Q kernels. It exposes the cudaq.kernel()
Expand Down
18 changes: 5 additions & 13 deletions python/cudaq/kernel/quake_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,11 @@
# This source code and the accompanying materials are made available under #
# the terms of the Apache License 2.0 which accompanies this distribution. #
# ============================================================================ #
from functools import partialmethod
import inspect
import sys
import random
import string
import numpy as np
import ctypes

from ..mlir.ir import *
from ..mlir.passmanager import *
from ..mlir.dialects import quake, cc
from ..mlir.dialects import builtin, func, arith
from ..mlir._mlir_libs._quakeDialects import cudaq_runtime

from cudaq.mlir._mlir_libs._quakeDialects import cudaq_runtime
from cudaq.mlir.dialects import arith, quake, cc
from cudaq.mlir.ir import (DenseI32ArrayAttr, F64Type, FloatAttr, Location,
IntegerAttr, IntegerType)
from .utils import mlirTypeFromPyType

qvector = cudaq_runtime.qvector
Expand Down
6 changes: 3 additions & 3 deletions python/cudaq/kernel/register_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
# the terms of the Apache License 2.0 which accompanies this distribution. #
# ============================================================================ #

from functools import partialmethod
import math
import numpy as np
from functools import partialmethod
from typing import Callable, List

from .utils import globalRegisteredOperations
from cudaq.mlir._mlir_libs._quakeDialects import cudaq_runtime
from .kernel_builder import PyKernel, __generalCustomOperation
from ..mlir._mlir_libs._quakeDialects import cudaq_runtime
from .utils import globalRegisteredOperations


def register_operation(operation_name: str, unitary):
Expand Down
18 changes: 9 additions & 9 deletions python/cudaq/kernel/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
# the terms of the Apache License 2.0 which accompanies this distribution. #
# ============================================================================ #
from __future__ import annotations

from ..mlir._mlir_libs._quakeDialects import cudaq_runtime
from ..mlir.dialects import quake, cc
from ..mlir.ir import *
from ..mlir.passmanager import *
import numpy as np
from typing import Callable, List
import ast, sys, traceback
import ast
import re
from typing import get_origin
import sys
import traceback
import numpy as np
from typing import get_origin, Callable, List

from cudaq.mlir._mlir_libs._quakeDialects import cudaq_runtime
from cudaq.mlir.dialects import quake, cc
from cudaq.mlir.ir import ComplexType, F32Type, F64Type, IntegerType

State = cudaq_runtime.State
qvector = cudaq_runtime.qvector
Expand Down
2 changes: 0 additions & 2 deletions python/cudaq/kernels/hwe.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
# This source code and the accompanying materials are made available under #
# the terms of the Apache License 2.0 which accompanies this distribution. #
# ============================================================================ #
import numpy as np
import cudaq


def num_hwe_parameters(numQubits, numLayers):
Expand Down
5 changes: 3 additions & 2 deletions python/cudaq/operator/cudm_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
# the terms of the Apache License 2.0 which accompanies this distribution. #
# ============================================================================ #

import logging
import numpy
from typing import Any, Mapping, List, Sequence, Union
from numbers import Number
from typing import Any, Mapping, List, Sequence, Union

from .expressions import ElementaryOperator, ScalarOperator
from .manipulation import OperatorArithmetics
import logging
from .schedule import Schedule

cudm = None
Expand Down
3 changes: 1 addition & 2 deletions python/cudaq/operator/cudm_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
from typing import Sequence, Mapping, List, Optional

from .cudm_helpers import CuDensityMatOpConversion, constructLiouvillian
from ..runtime.observe import observe
from .schedule import Schedule
from .expressions import Operator
from ..mlir._mlir_libs._quakeDialects import cudaq_runtime
from cudaq.mlir._mlir_libs._quakeDialects import cudaq_runtime
from .cudm_helpers import cudm, CudmStateType
from .cudm_state import CuDensityMatState, as_cudm_state
from .helpers import InitialState, InitialStateArgT
Expand Down
10 changes: 6 additions & 4 deletions python/cudaq/operator/cudm_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
# the terms of the Apache License 2.0 which accompanies this distribution. #
# ============================================================================ #

from cuquantum.densitymat import DenseMixedState, DensePureState, WorkStream
import numpy, cupy, atexit
from typing import Sequence
import cupy
import numpy
from cupy.cuda.memory import MemoryPointer, UnownedMemory
from ..mlir._mlir_libs._quakeDialects import cudaq_runtime
from cuquantum.bindings import cudensitymat as cudm
from cuquantum.densitymat import DenseMixedState, DensePureState, WorkStream
from typing import Sequence

from cudaq.mlir._mlir_libs._quakeDialects import cudaq_runtime
from .helpers import InitialState


Expand Down
8 changes: 5 additions & 3 deletions python/cudaq/operator/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
# the terms of the Apache License 2.0 which accompanies this distribution. #
# ============================================================================ #

import numpy, scipy # type: ignore
import numpy
import scipy
from numpy.typing import NDArray
from typing import Sequence

from cudaq.mlir._mlir_libs._quakeDialects import cudaq_runtime
from .helpers import NumericType
from .expressions import OperatorSum, ProductOperator, ElementaryOperator, ScalarOperator, RydbergHamiltonian
from ..mlir._mlir_libs._quakeDialects import cudaq_runtime
from .expressions import (OperatorSum, ProductOperator, ElementaryOperator,
ScalarOperator)


# Operators as defined here (watch out of differences in convention):
Expand Down
17 changes: 8 additions & 9 deletions python/cudaq/operator/evolution.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,22 @@
# ============================================================================ #

from __future__ import annotations
import numpy, scipy, sys, uuid
from numpy.typing import NDArray
from typing import Callable, Iterable, Mapping, Optional, Sequence
import json
import numpy
import random
import string
import warnings
import uuid
from numpy.typing import NDArray
from typing import Callable, Iterable, Mapping, Optional, Sequence

from cudaq.kernel.kernel_builder import PyKernel, make_kernel
from cudaq.kernel.register_op import register_operation
from cudaq.kernel.utils import ahkPrefix
from cudaq.mlir._mlir_libs._quakeDialects import cudaq_runtime
from .expressions import Operator, RydbergHamiltonian
from .helpers import NumericType, InitialState, InitialStateArgT
from .integrator import BaseIntegrator
from .schedule import Schedule
from ..kernel.register_op import register_operation
from ..kernel.utils import ahkPrefix
from ..mlir._mlir_libs._quakeDialects import cudaq_runtime
from ..kernel.kernel_builder import PyKernel, make_kernel
from ..runtime.observe import observe


def _taylor_series_expm(op_matrix: NDArray[numpy.complexfloating],
Expand Down
Loading

0 comments on commit 77ba55a

Please sign in to comment.