Skip to content

perf: vectorize a loop-based matrix constructor #1887

Description

@vprusso

A few small matrix constructors build their result with a Python for loop over a preallocated np.zeros/np.empty, where numpy vectorization would be faster and clearer.

What to do

Pick ONE of the following and replace its loop with a vectorized construction:

  • toqito/matrices/standard_basis.py
  • toqito/matrices/gen_pauli_z.py
  • toqito/matrices/entangled_subspace.py
  • toqito/perms/perfect_matchings.py

Requirements

  • The output must be bit-for-bit identical to the current implementation. Keep the existing test, and add a case comparing the new output to the previous one if helpful.
  • Include a measured speedup on a representative size (a short timeit snippet in the PR description is enough).
  • Do not change the function's signature or behavior.

This one needs some numpy comfort, so it is a good pick if you want a slightly meatier first PR.

Verifying

uv run pytest toqito/matrices/tests toqito/perms/tests

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions