Skip to content

Commit

Permalink
Merge branch 'main' of github.com:Qiskit/qiskit into bit-visibility
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinhartman committed Nov 11, 2024
2 parents 9a4a781 + 3a9993a commit 1663c13
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 40 deletions.
10 changes: 5 additions & 5 deletions crates/accelerate/src/twirling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ fn twirl_gate(
params: None,
extra_attrs: ExtraInstructionAttributes::new(None, None, None, None),
#[cfg(feature = "cache_pygates")]
py_op: std::cell::OnceCell::new(),
py_op: std::sync::OnceLock::new(),
},
)?;
out_circ.push(
Expand All @@ -221,7 +221,7 @@ fn twirl_gate(
params: None,
extra_attrs: ExtraInstructionAttributes::new(None, None, None, None),
#[cfg(feature = "cache_pygates")]
py_op: std::cell::OnceCell::new(),
py_op: std::sync::OnceLock::new(),
},
)?;

Expand All @@ -235,7 +235,7 @@ fn twirl_gate(
params: None,
extra_attrs: ExtraInstructionAttributes::new(None, None, None, None),
#[cfg(feature = "cache_pygates")]
py_op: std::cell::OnceCell::new(),
py_op: std::sync::OnceLock::new(),
},
)?;
out_circ.push(
Expand All @@ -247,7 +247,7 @@ fn twirl_gate(
params: None,
extra_attrs: ExtraInstructionAttributes::new(None, None, None, None),
#[cfg(feature = "cache_pygates")]
py_op: std::cell::OnceCell::new(),
py_op: std::sync::OnceLock::new(),
},
)?;

Expand Down Expand Up @@ -361,7 +361,7 @@ fn generate_twirled_circuit(
)),
extra_attrs: inst.extra_attrs.clone(),
#[cfg(feature = "cache_pygates")]
py_op: std::cell::OnceCell::new(),
py_op: std::sync::OnceLock::new(),
};
#[cfg(feature = "cache_pygates")]
new_inst.py_op.set(new_inst_obj).unwrap();
Expand Down
6 changes: 3 additions & 3 deletions crates/accelerate/src/unitary_synthesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
// that they have been altered from the originals.
#![allow(clippy::too_many_arguments)]

#[cfg(feature = "cache_pygates")]
use std::cell::OnceCell;
use std::f64::consts::PI;
#[cfg(feature = "cache_pygates")]
use std::sync::OnceLock;

use approx::relative_eq;
use hashbrown::{HashMap, HashSet};
Expand Down Expand Up @@ -149,7 +149,7 @@ fn apply_synth_sequence(
params: new_params,
extra_attrs: ExtraInstructionAttributes::default(),
#[cfg(feature = "cache_pygates")]
py_op: OnceCell::new(),
py_op: OnceLock::new(),
};
instructions.push(instruction);
}
Expand Down
12 changes: 6 additions & 6 deletions crates/circuit/src/circuit_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// that they have been altered from the originals.

#[cfg(feature = "cache_pygates")]
use std::cell::OnceCell;
use std::sync::OnceLock;

use crate::bit_data::BitData;
use crate::circuit_instruction::{
Expand Down Expand Up @@ -304,7 +304,7 @@ impl CircuitData {
params: inst.params.clone(),
extra_attrs: inst.extra_attrs.clone(),
#[cfg(feature = "cache_pygates")]
py_op: OnceCell::new(),
py_op: OnceLock::new(),
});
}
} else if copy_instructions {
Expand All @@ -316,7 +316,7 @@ impl CircuitData {
params: inst.params.clone(),
extra_attrs: inst.extra_attrs.clone(),
#[cfg(feature = "cache_pygates")]
py_op: OnceCell::new(),
py_op: OnceLock::new(),
});
}
} else {
Expand Down Expand Up @@ -941,7 +941,7 @@ impl CircuitData {
params,
extra_attrs: ExtraInstructionAttributes::default(),
#[cfg(feature = "cache_pygates")]
py_op: OnceCell::new(),
py_op: OnceLock::new(),
});
res.track_instruction_parameters(py, res.data.len() - 1)?;
}
Expand Down Expand Up @@ -1089,7 +1089,7 @@ impl CircuitData {
params,
extra_attrs: ExtraInstructionAttributes::default(),
#[cfg(feature = "cache_pygates")]
py_op: OnceCell::new(),
py_op: OnceLock::new(),
});
res.track_instruction_parameters(py, res.data.len() - 1)?;
}
Expand Down Expand Up @@ -1147,7 +1147,7 @@ impl CircuitData {
params,
extra_attrs: ExtraInstructionAttributes::default(),
#[cfg(feature = "cache_pygates")]
py_op: OnceCell::new(),
py_op: OnceLock::new(),
});
Ok(())
}
Expand Down
6 changes: 3 additions & 3 deletions crates/circuit/src/circuit_instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// that they have been altered from the originals.

#[cfg(feature = "cache_pygates")]
use std::cell::OnceCell;
use std::sync::OnceLock;

use numpy::IntoPyArray;
use pyo3::basic::CompareOp;
Expand Down Expand Up @@ -236,7 +236,7 @@ pub struct CircuitInstruction {
pub params: SmallVec<[Param; 3]>,
pub extra_attrs: ExtraInstructionAttributes,
#[cfg(feature = "cache_pygates")]
pub py_op: OnceCell<Py<PyAny>>,
pub py_op: OnceLock<Py<PyAny>>,
}

impl CircuitInstruction {
Expand Down Expand Up @@ -301,7 +301,7 @@ impl CircuitInstruction {
params,
extra_attrs: ExtraInstructionAttributes::new(label, None, None, None),
#[cfg(feature = "cache_pygates")]
py_op: OnceCell::new(),
py_op: OnceLock::new(),
})
}

Expand Down
2 changes: 1 addition & 1 deletion crates/circuit/src/converters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// that they have been altered from the originals.

#[cfg(feature = "cache_pygates")]
use std::cell::OnceCell;
use std::sync::OnceLock;

use ::pyo3::prelude::*;
use hashbrown::HashMap;
Expand Down
18 changes: 9 additions & 9 deletions crates/circuit/src/dag_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ use std::convert::Infallible;
use std::f64::consts::PI;

#[cfg(feature = "cache_pygates")]
use std::cell::OnceCell;
use std::sync::OnceLock;

static CONTROL_FLOW_OP_NAMES: [&str; 4] = ["for_loop", "while_loop", "if_else", "switch_case"];
static SEMANTIC_EQ_SYMMETRIC: [&str; 4] = ["barrier", "swap", "break_loop", "continue_loop"];
Expand Down Expand Up @@ -5131,7 +5131,7 @@ impl DAGCircuit {
let py_op = if let Some(py_op) = py_op {
py_op.into()
} else {
OnceCell::new()
OnceLock::new()
};
let packed_instruction = PackedInstruction {
op,
Expand Down Expand Up @@ -6191,7 +6191,7 @@ impl DAGCircuit {
.then(|| Box::new(new_gate.1.iter().map(|x| Param::Float(*x)).collect())),
extra_attrs: ExtraInstructionAttributes::default(),
#[cfg(feature = "cache_pygates")]
py_op: OnceCell::new(),
py_op: OnceLock::new(),
}
} else {
panic!("This method only works if provided index is an op node");
Expand Down Expand Up @@ -6274,12 +6274,12 @@ impl DAGCircuit {
};
#[cfg(feature = "cache_pygates")]
let py_op = match new_op.operation.view() {
OperationRef::Standard(_) => OnceCell::new(),
OperationRef::Gate(gate) => OnceCell::from(gate.gate.clone_ref(py)),
OperationRef::Standard(_) => OnceLock::new(),
OperationRef::Gate(gate) => OnceLock::from(gate.gate.clone_ref(py)),
OperationRef::Instruction(instruction) => {
OnceCell::from(instruction.instruction.clone_ref(py))
OnceLock::from(instruction.instruction.clone_ref(py))
}
OperationRef::Operation(op) => OnceCell::from(op.operation.clone_ref(py)),
OperationRef::Operation(op) => OnceLock::from(op.operation.clone_ref(py)),
};
let inst = PackedInstruction {
op: new_op.operation,
Expand Down Expand Up @@ -6730,7 +6730,7 @@ impl DAGCircuit {
params: instr.params.clone(),
extra_attrs: instr.extra_attrs.clone(),
#[cfg(feature = "cache_pygates")]
py_op: OnceCell::new(),
py_op: OnceLock::new(),
})
})
.collect::<PyResult<Vec<_>>>()?;
Expand Down Expand Up @@ -6992,7 +6992,7 @@ impl DAGCircuit {
params: (!new_op.params.is_empty()).then(|| new_op.params.into()),
extra_attrs,
#[cfg(feature = "cache_pygates")]
py_op: py_op_cache.map(OnceCell::from).unwrap_or_default(),
py_op: py_op_cache.map(OnceLock::from).unwrap_or_default(),
});
if let Some(weight) = self.dag.node_weight_mut(node_index) {
*weight = new_weight;
Expand Down
8 changes: 4 additions & 4 deletions crates/circuit/src/dag_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
// copyright notice, and modified files need to carry a notice indicating
// that they have been altered from the originals.

#[cfg(feature = "cache_pygates")]
use std::cell::OnceCell;
use std::hash::Hasher;
#[cfg(feature = "cache_pygates")]
use std::sync::OnceLock;

use crate::circuit_instruction::{CircuitInstruction, OperationFromPython};
use crate::imports::QUANTUM_CIRCUIT;
Expand Down Expand Up @@ -241,7 +241,7 @@ impl DAGOpNode {
instruction.operation = instruction.operation.py_deepcopy(py, None)?;
#[cfg(feature = "cache_pygates")]
{
instruction.py_op = OnceCell::new();
instruction.py_op = OnceLock::new();
}
}
let base = PyClassInitializer::from(DAGNode { node: None });
Expand Down Expand Up @@ -293,7 +293,7 @@ impl DAGOpNode {
params: self.instruction.params.clone(),
extra_attrs: self.instruction.extra_attrs.clone(),
#[cfg(feature = "cache_pygates")]
py_op: OnceCell::new(),
py_op: OnceLock::new(),
})
}

Expand Down
14 changes: 7 additions & 7 deletions crates/circuit/src/packed_instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
// copyright notice, and modified files need to carry a notice indicating
// that they have been altered from the originals.

#[cfg(feature = "cache_pygates")]
use std::cell::OnceCell;
use std::ptr::NonNull;
#[cfg(feature = "cache_pygates")]
use std::sync::OnceLock;

use pyo3::intern;
use pyo3::prelude::*;
Expand Down Expand Up @@ -504,17 +504,17 @@ pub struct PackedInstruction {
pub extra_attrs: ExtraInstructionAttributes,

#[cfg(feature = "cache_pygates")]
/// This is hidden in a `OnceCell` because it's just an on-demand cache; we don't create this
/// unless asked for it. A `OnceCell` of a non-null pointer type (like `Py<T>`) is the same
/// This is hidden in a `OnceLock` because it's just an on-demand cache; we don't create this
/// unless asked for it. A `OnceLock` of a non-null pointer type (like `Py<T>`) is the same
/// size as a pointer and there are no runtime checks on access beyond the initialisation check,
/// which is a simple null-pointer check.
///
/// WARNING: remember that `OnceCell`'s `get_or_init` method is no-reentrant, so the initialiser
/// WARNING: remember that `OnceLock`'s `get_or_init` method is no-reentrant, so the initialiser
/// must not yield the GIL to Python space. We avoid using `GILOnceCell` here because it
/// requires the GIL to even `get` (of course!), which makes implementing `Clone` hard for us.
/// We can revisit once we're on PyO3 0.22+ and have been able to disable its `py-clone`
/// feature.
pub py_op: OnceCell<Py<PyAny>>,
pub py_op: OnceLock<Py<PyAny>>,
}

impl PackedInstruction {
Expand Down Expand Up @@ -581,7 +581,7 @@ impl PackedInstruction {
}
};

// `OnceCell::get_or_init` and the non-stabilised `get_or_try_init`, which would otherwise
// `OnceLock::get_or_init` and the non-stabilised `get_or_try_init`, which would otherwise
// be nice here are both non-reentrant. This is a problem if the init yields control to the
// Python interpreter as this one does, since that can allow CPython to freeze the thread
// and for another to attempt the initialisation.
Expand Down
8 changes: 8 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@
napoleon_google_docstring = True
napoleon_numpy_docstring = False

# Autosummary generates stub filenames based on the import name.
# Sometimes, two distinct interfaces only differ in capitalization; this
# creates a problem on case-insensitive OS/filesystems like macOS. So,
# we manually avoid the clash by renaming one of the files.
autosummary_filename_map = {
"qiskit.circuit.library.iqp": "qiskit.circuit.library.iqp_function",
}


# ----------------------------------------------------------------------------------
# Doctest
Expand Down
4 changes: 2 additions & 2 deletions test/benchmarks/manipulate.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import os

from qiskit import QuantumCircuit
from qiskit.circuit import twirl_circuit
from qiskit.circuit import pauli_twirl_2q_gates
from qiskit.passmanager import PropertySet
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
from .utils import multi_control_circuit
Expand All @@ -38,7 +38,7 @@ def time_DTC100_twirling(self):
"""Perform Pauli-twirling on a 100Q QV
circuit
"""
out = twirl_circuit(self.dtc_qc, seed=12345678942)
out = pauli_twirl_2q_gates(self.dtc_qc, seed=12345678942)
return out

def time_multi_control_decompose(self):
Expand Down

0 comments on commit 1663c13

Please sign in to comment.