Skip to content

Implement the qargs temporary state for SparseObservable #13391

@jakelishman

Description

@jakelishman

What should we add?

Most quantum_info operators, including SparsePauliOp, have a qargs argument in several of their mathematical methods, and have a __call__ method that creates a shallow copy of the SparsePauliOp with the qargs temporarily set, for use with the Python binary operators (like +). The Python version of SparseObservable should also gain this functionality.

A qargs keyword argument to certain methods is straightforwards enough. Making the shallow-copy-on-__call__ behaviour is more challenging because the data is owned by Rust, and can't be easily shared like it can in Python. Likely we will want to make an entirely clean split between the Rust-space SparseObservable and the Python-exposed version (which I'll call PySparseObservable, but its Python name will also be SparseObservable) that contains something like an Arc<RwLock<SparseObservable>> and whatever shallow additional Python state we want. Something like:

// The same as it is now.  *Not* a pyclass.
pub struct SparseObservable {
    num_qubits: u32,
    coeffs: Vec<Complex64>,
    bit_terms: Vec<BitTerm>,
    indices: Vec<u32>,
    boundaries: Vec<usize>,
}
impl SparseObservable {
    // All the actual worker methods on the struct.
}

#[pyclass(name="SparseObservable")]
struct PySparseObservable {
    inner: Arc<RwLock<SparseObservable>>,
    qargs: Option<Vec<u32>>,
}
#[pymethods]
impl PySparseObservable {
    // All the Python methods.  These should generally be thin wrappers around proper
    // `SparseObservable` Rust-space methods, and the pymethods only handle interaction
    // with Python.
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    mod: quantum infoRelated to the Quantum Info module (States & Operators)type: feature requestNew feature or request

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions