Skip to content

Commit

Permalink
Merge branch 'main' into add_random_circuit_with_graph
Browse files Browse the repository at this point in the history
  • Loading branch information
MozammilQ authored Feb 5, 2025
2 parents aabacb9 + 69bb439 commit 7c1cca7
Show file tree
Hide file tree
Showing 43 changed files with 586 additions and 5,515 deletions.
13 changes: 3 additions & 10 deletions .azure/test-linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,20 +96,13 @@ jobs:
sudo apt-get install -y graphviz
displayName: 'Install optional non-Python dependencies'
# Note that we explicitly use the virtual env with Qiskit installed to run the Rust
# tests since some of them still depend on Qiskit's Python API via PyO3.
- ${{ if eq(parameters.testRust, true) }}:
# We need to avoid linking our crates into full Python extension libraries during Rust-only
# testing because Rust/PyO3 can't handle finding a static CPython interpreter.
# Note that we use the virtual env with Qiskit installed to run the Rust
# tests since some of them still depend on Qiskit's Python API via PyO3.
- bash: |
source test-job/bin/activate
python tools/report_numpy_state.py
PYTHONUSERBASE="$VIRTUAL_ENV" cargo test --no-default-features
env:
# On Linux we link against `libpython` dynamically, but it isn't written into the rpath
# of the test executable (I'm not 100% sure why ---Jake). It's easiest just to forcibly
# include the correct place in the `dlopen` search path.
LD_LIBRARY_PATH: '$(usePython.pythonLocation)/lib:$LD_LIBRARY_PATH'
python tools/run_cargo_test.py
displayName: "Run Rust tests"
- bash: |
Expand Down
18 changes: 3 additions & 15 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -619,25 +619,13 @@ Then, run the following commands:

```bash
python setup.py build_rust --inplace
PYTHONUSERBASE="$VIRTUAL_ENV" cargo test --no-default-features
tools/run_cargo_test.py
```

> [!IMPORTANT]
> On Linux, you may need to first set your `LD_LIBRARY_PATH` env var to include the
> path to your Python installation's shared lib, e.g.:
> ```bash
> export LD_LIBRARY_PATH="$(python -c 'import sysconfig; print(sysconfig.get_config_var("LIBDIR"))'):$LD_LIBRARY_PATH"
> ```
The first command builds Qiskit in editable mode,
which ensures that Rust tests that interact with Qiskit's Python code actually
use the latest Python code from your working directory.
The second command actually invokes the tests via Cargo. The `PYTHONUSERBASE`
environment variable tells the embedded Python interpreter to look for packages
in your active virtual environment. The `--no-default-features`
flag is used to compile an isolated test runner without building a linked CPython
extension module (which would otherwise cause linker failures).
use the latest Python code from your working directory. The second command invokes
the tests via Cargo.

#### Calling Python from Rust tests
By default, our Cargo project configuration allows Rust tests to interact with the
Expand Down
60 changes: 33 additions & 27 deletions crates/accelerate/src/sparse_observable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2956,23 +2956,23 @@ impl PySparseObservable {
other: &Bound<'py, PyAny>,
) -> PyResult<Bound<'py, PyAny>> {
let py = slf_.py();
if slf_.is(other) {
let Some(other) = coerce_to_observable(other)? else {
return Ok(py.NotImplemented().into_bound(py));
};

let other = other.borrow();
let slf_ = slf_.borrow();
if Arc::ptr_eq(&slf_.inner, &other.inner) {
// This fast path is for consistency with the in-place `__iadd__`, which would otherwise
// struggle to do the addition to itself.
let slf_ = slf_.borrow();
let inner = slf_.inner.read().map_err(|_| InnerReadError)?;
return <&SparseObservable as ::std::ops::Mul<_>>::mul(
&inner,
Complex64::new(2.0, 0.0),
)
.into_bound_py_any(py);
}
let Some(other) = coerce_to_observable(other)? else {
return Ok(py.NotImplemented().into_bound(py));
};
let slf_ = slf_.borrow();
let slf_inner = slf_.inner.read().map_err(|_| InnerReadError)?;
let other = other.borrow();
let other_inner = other.inner.read().map_err(|_| InnerReadError)?;
slf_inner.check_equal_qubits(&other_inner)?;
<&SparseObservable as ::std::ops::Add>::add(&slf_inner, &other_inner).into_bound_py_any(py)
Expand All @@ -2992,13 +2992,7 @@ impl PySparseObservable {
<&SparseObservable as ::std::ops::Add>::add(&other_inner, &inner).into_bound_py_any(py)
}

fn __iadd__(slf_: Bound<Self>, other: &Bound<PyAny>) -> PyResult<()> {
if slf_.is(other) {
let slf_ = slf_.borrow();
let mut slf_inner = slf_.inner.write().map_err(|_| InnerWriteError)?;
*slf_inner *= Complex64::new(2.0, 0.0);
return Ok(());
}
fn __iadd__(slf_: Bound<PySparseObservable>, other: &Bound<PyAny>) -> PyResult<()> {
let Some(other) = coerce_to_observable(other)? else {
// This is not well behaved - we _should_ return `NotImplemented` to Python space
// without an exception, but limitations in PyO3 prevent this at the moment. See
Expand All @@ -3008,9 +3002,18 @@ impl PySparseObservable {
other.repr()?
)));
};

let other = other.borrow();
let slf_ = slf_.borrow();
let mut slf_inner = slf_.inner.write().map_err(|_| InnerWriteError)?;
let other = other.borrow();

// Check if slf_ and other point to the same SparseObservable object, in which case
// we just multiply it by 2
if Arc::ptr_eq(&slf_.inner, &other.inner) {
*slf_inner *= Complex64::new(2.0, 0.0);
return Ok(());
}

let other_inner = other.inner.read().map_err(|_| InnerReadError)?;
slf_inner.check_equal_qubits(&other_inner)?;
slf_inner.add_assign(&other_inner);
Expand All @@ -3022,16 +3025,17 @@ impl PySparseObservable {
other: &Bound<'py, PyAny>,
) -> PyResult<Bound<'py, PyAny>> {
let py = slf_.py();
if slf_.is(other) {
return PySparseObservable::zero(slf_.borrow().num_qubits()?).into_bound_py_any(py);
}
let Some(other) = coerce_to_observable(other)? else {
return Ok(py.NotImplemented().into_bound(py));
};

let other = other.borrow();
let slf_ = slf_.borrow();
if Arc::ptr_eq(&slf_.inner, &other.inner) {
return PySparseObservable::zero(slf_.num_qubits()?).into_bound_py_any(py);
}

let slf_inner = slf_.inner.read().map_err(|_| InnerReadError)?;
let other = other.borrow();
let other_inner = other.inner.read().map_err(|_| InnerReadError)?;
slf_inner.check_equal_qubits(&other_inner)?;
<&SparseObservable as ::std::ops::Sub>::sub(&slf_inner, &other_inner).into_bound_py_any(py)
Expand All @@ -3050,13 +3054,6 @@ impl PySparseObservable {
}

fn __isub__(slf_: Bound<PySparseObservable>, other: &Bound<PyAny>) -> PyResult<()> {
if slf_.is(other) {
// This is not strictly the same thing as `a - a` if `a` contains non-finite
// floating-point values (`inf - inf` is `NaN`, for example); we don't really have a
// clear view on what floating-point guarantees we're going to make right now.
slf_.borrow_mut().clear()?;
return Ok(());
}
let Some(other) = coerce_to_observable(other)? else {
// This is not well behaved - we _should_ return `NotImplemented` to Python space
// without an exception, but limitations in PyO3 prevent this at the moment. See
Expand All @@ -3066,9 +3063,18 @@ impl PySparseObservable {
other.repr()?
)));
};
let other = other.borrow();
let slf_ = slf_.borrow();
let mut slf_inner = slf_.inner.write().map_err(|_| InnerWriteError)?;
let other = other.borrow();

if Arc::ptr_eq(&slf_.inner, &other.inner) {
// This is not strictly the same thing as `a - a` if `a` contains non-finite
// floating-point values (`inf - inf` is `NaN`, for example); we don't really have a
// clear view on what floating-point guarantees we're going to make right now.
slf_inner.clear();
return Ok(());
}

let other_inner = other.inner.read().map_err(|_| InnerReadError)?;
slf_inner.check_equal_qubits(&other_inner)?;
slf_inner.sub_assign(&other_inner);
Expand Down
6 changes: 0 additions & 6 deletions docs/apidoc/assembler.rst

This file was deleted.

1 change: 0 additions & 1 deletion docs/apidoc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ Other:
.. toctree::
:maxdepth: 1

assembler
compiler
exceptions
qobj
Expand Down
3 changes: 1 addition & 2 deletions qiskit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@

_config = _user_config.get_config()

from qiskit.compiler import transpile, assemble, schedule, sequence
from qiskit.compiler import transpile, schedule, sequence
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
from .version import __version__

Expand All @@ -138,7 +138,6 @@
"QiskitError",
"QuantumCircuit",
"QuantumRegister",
"assemble",
"schedule",
"sequence",
"transpile",
Expand Down
42 changes: 0 additions & 42 deletions qiskit/assembler/__init__.py

This file was deleted.

Loading

0 comments on commit 7c1cca7

Please sign in to comment.