genQC logo genQC logo genQC
  • Overview
  • Get Started
  • Tutorials
  • API Reference
  • Research
  • Code Repository
  1. Utils
  2. Miscellaneous util

API Reference

  • Modules Overview
  • Release notes

  • Benchmark
    • Compilation benchmark
  • Dataset
    • Dataset balancing
    • Cached dataset
    • Quantum circuit dataset
    • Config dataset
    • Dataset helper functions
    • Mixed cached dataset
  • Inference
    • Evaluation metrics
    • Evaluation helper
    • Sampling functions
  • Models
    • Config model
    • Frozen OpenCLIP
    • Layers
    • Position encodings
    • Conditional qc-UNet
    • Encoder for unitaries
    • Clip
      • Frozen OpenCLIP
      • Unitary CLIP
    • Embedding
      • Base embedder
      • Rotational preset embedder
    • Transformers
      • Transformers and attention
      • CirDiT - Circuit Diffusion Transformer
      • Transformers
  • Pipeline
    • Callbacks
    • Compilation Diffusion Pipeline
    • Diffusion Pipeline
    • Diffusion Pipeline Special
    • Metrics
    • Multimodal Diffusion Pipeline
    • Pipeline
    • Unitary CLIP Pipeline
  • Platform
    • Circuits dataset generation functions
    • Circuits instructions
    • Simulation backend
    • Backends
      • Base backend
      • CUDA-Q circuits backend
      • Pennylane circuits backend
      • Qiskit circuits backend
    • Tokenizer
      • Base tokenizer
      • Circuits tokenizer
      • Tensor tokenizer
  • Scheduler
    • Scheduler
    • DDIM Scheduler
    • DDPM Scheduler
    • DPM Scheduler
  • Utils
    • Async functions
    • Config loader
    • Math and algorithms
    • Miscellaneous util

On this page

  • Memory utils
    • MemoryCleaner
  • Python utils
    • virtual
    • cache_data
  • Torch utils
    • DataLoaders
    • infer_torch_device
    • number_of_paramters
    • scale_tensor
    • normalize_tensor
  • Plot utils
    • saveSvg
    • savePng
    • savePdf
    • plot_image_grid
    • latents_to_pil
  • Inference utils
    • set_seed
    • get_element_matching_indices
    • get_entanglement_bins
  • Report an issue
  • View source
  1. Utils
  2. Miscellaneous util

Miscellaneous util

Miscellaneous util code

Memory utils


source

MemoryCleaner


def MemoryCleaner(
    args:VAR_POSITIONAL, kwargs:VAR_KEYWORD
):

CLass with static methods to clean (gpu) memory.

Python utils


source

virtual


def virtual(
    f:callable
)->callable:

Decorator to enfore subclass method implementations and raises error at method calls.

class A():
    def p1(self, x): print("A p1", x)
    
    @virtual
    def p2(self, x): pass
 
class B(A):
    def p3(self, x): print("B p2", x)
    
b = B()
b.p1(1)
try:
    b.p2(1)
except BaseException as e:
    print("Exception that would be raised: ", e)
A p1 1
Exception that would be raised:  Virtual method p2 needs to be implemented by subclass B.

source

cache_data


def cache_data(
    file_name, force_recompute
):

A decorator that memorizes the result of a function and stores it. Note, if the function or its arguments change we ignore it, we only check if the file exists!

Parameters: - file_name (str): The name of the file to store the memoized results. - force_recompute (bool): If True, existing cache is ignored.

Torch utils


source

DataLoaders


def DataLoaders(
    dls:list
):

Combines train and valid DataLoader objects.


source

infer_torch_device


def infer_torch_device(
    
):
infer_torch_device()
[INFO]: Cuda device has a capability of 8.6 (>= 8), allowing tf32 matmul.
device(type='cuda')

source

number_of_paramters


def number_of_paramters(
    model:Module
):

source

scale_tensor


def scale_tensor(
    t:Tensor
):

[-1,1] to [0,1]


source

normalize_tensor


def normalize_tensor(
    t:Tensor
):

[0,1] to [-1,1]

Plot utils


source

saveSvg


def saveSvg(
    filename
):

source

savePng


def savePng(
    filename
):

source

savePdf


def savePdf(
    filename
):

source

plot_image_grid


def plot_image_grid(
    imgs:Union, labels:list=None, labels_fs:str='medium', figsize:tuple=(16, 4), cols:int=8, cmap:str='Greys',
    show_colorbar:bool=False, imshow_kwargs:VAR_KEYWORD
):
n = 6
plot_image_grid(torch.randn((n,28,28,1)), [f"label {i}" for i in range(n)])


source

latents_to_pil


def latents_to_pil(
    latents:Tensor, channels:NoneType=None
):

Inference utils


source

set_seed


def set_seed(
    seed:int
):

Sets a seed to pytorch, numpy and python. Additionally sets cuda flags.


source

get_element_matching_indices


def get_element_matching_indices(
    a:Tensor, b:Tensor
)->Tensor:

Compares (2d) a with b. Returns the indices of b, where a element of a matches with b.


source

get_entanglement_bins


def get_entanglement_bins(
    num_of_qubits:int
)->Tuple:

Returns all SRV sorted in entangle bins, corresponding to a number of entangled qubits.

Print the Schmidt-rank-vector bins for 5 qubits:

for srvs,label in zip(*get_entanglement_bins(5)):
    print(label, ":", srvs)
0 qubit entangled : [[1, 1, 1, 1, 1]]
2 qubit entangled : [[1, 1, 1, 2, 2], [1, 1, 2, 1, 2], [1, 1, 2, 2, 1], [1, 2, 1, 1, 2], [1, 2, 1, 2, 1], [1, 2, 2, 1, 1], [2, 1, 1, 1, 2], [2, 1, 1, 2, 1], [2, 1, 2, 1, 1], [2, 2, 1, 1, 1]]
3 qubit entangled : [[1, 1, 2, 2, 2], [1, 2, 1, 2, 2], [1, 2, 2, 1, 2], [1, 2, 2, 2, 1], [2, 1, 1, 2, 2], [2, 1, 2, 1, 2], [2, 1, 2, 2, 1], [2, 2, 1, 1, 2], [2, 2, 1, 2, 1], [2, 2, 2, 1, 1]]
4 qubit entangled : [[1, 2, 2, 2, 2], [2, 1, 2, 2, 2], [2, 2, 1, 2, 2], [2, 2, 2, 1, 2], [2, 2, 2, 2, 1]]
5 qubit entangled : [[2, 2, 2, 2, 2]]
Back to top
Math and algorithms
 

Copyright 2025, Florian Fürrutter

  • Report an issue
  • View source