pub struct TwoLocal { /* private fields */ }Expand description
The TwoLocal ansatz, a versatile hardware-efficient parameterized circuit.
TwoLocal is a popular ansatz architecture consisting of alternating layers of single-qubit rotations and two-qubit entanglement gates. It serves as the foundation for many common ansatz patterns including RealAmplitudes and EfficientSU2.
§Architecture
The circuit structure follows this pattern:
Layer 0: [R]───────[R]───────[R]───────[R] (Rotation)
│ │ │ │
Layer 0.5: └─[E]───┘ └─[E]───┘ (Entanglement)
Layer 1: [R]───────[R]───────[R]───────[R]
│ │ │ │
Layer 1.5: └─[E]───┘ └─[E]───┘
...Where [R] represents parameterized rotation gates and [E] represents
entanglement gates.
§Configuration
Use the builder pattern to configure the ansatz:
reps: Number of repetition layersrotation_gates: Single-qubit gates (e.g.,[RY],[RY, RZ])entanglement_gate: Two-qubit gate (e.g.,CX,CZ)entanglement: Connectivity patternskip_final_rotation_layer: Omit final rotation
§Parameter Count
The total number of parameters is:
layers × num_qubits × rotation_gates.len()
where layers = reps if skip_final_rotation_layer is true,
otherwise layers = reps + 1.
Implementations§
Source§impl TwoLocal
impl TwoLocal
Sourcepub fn new(num_qubits: usize) -> Self
pub fn new(num_qubits: usize) -> Self
Creates a new TwoLocal ansatz with sensible defaults.
§Default Configuration
reps: 3 layersrotation_gates:[RY]entanglement_gate:CXentanglement:Linearskip_final_rotation_layer: false
§Arguments
num_qubits- The number of qubits in the ansatz.
§Example
use cqlib_core::circuit::ansatz::{Ansatz, TwoLocal};
let ansatz = TwoLocal::new(5);
assert_eq!(ansatz.num_qubits(), 5);Sourcepub fn reps(self, reps: usize) -> Self
pub fn reps(self, reps: usize) -> Self
Sets the number of repetition layers.
Each repetition consists of one rotation layer followed by one entanglement layer. The default is 3.
§Arguments
reps- The number of repetitions (must be >= 0).
Sourcepub fn rotation_gates(self, gates: Vec<StandardGate>) -> Self
pub fn rotation_gates(self, gates: Vec<StandardGate>) -> Self
Sets the rotation gates used in the rotation layers.
Each qubit receives all specified rotation gates in sequence. Valid choices are single-parameter single-qubit rotation gates:
[RX],[RY],[RZ]individually[RY, RZ]for EfficientSU2 ansatz[RX, RY, RZ]for full SU(2) coverage
§Arguments
gates- A vector of single-qubit parameterized gates (RX, RY, or RZ).
§Errors
Validation is performed in build_circuit.
Returns CircuitError::InvalidOperation if any gate is not RX, RY, or RZ.
Sourcepub fn entanglement_gate(self, gate: StandardGate) -> Self
pub fn entanglement_gate(self, gate: StandardGate) -> Self
Sets the entanglement gate used in the entanglement layers.
Valid choices are two-qubit controlled gates that create entanglement:
CX(CNOT): Standard choice, universal for entanglement (default)CY: Controlled-Y gateCZ: Symmetric, native on some hardware
§Arguments
gate- A two-qubit controlled entanglement gate (CX, CY, or CZ).
§Errors
Validation is performed in build_circuit.
Returns CircuitError::InvalidOperation if the gate is not CX, CY, or CZ.
Sourcepub fn entanglement(self, topology: EntanglementTopology) -> Self
pub fn entanglement(self, topology: EntanglementTopology) -> Self
Sets the entanglement topology.
See EntanglementTopology for available options.
§Arguments
topology- The desired connectivity pattern.
Sourcepub fn skip_final_rotation_layer(self, skip: bool) -> Self
pub fn skip_final_rotation_layer(self, skip: bool) -> Self
Sets whether to skip the final rotation layer.
The default TwoLocal architecture adds a final rotation layer after
the last entanglement layer (total reps + 1 rotation layers).
When enabled, this final rotation layer is omitted, meaning the
ansatz ends with an entanglement layer and has exactly reps
rotation layers.
§Arguments
skip- Iftrue, omit the final rotation layer.
Trait Implementations§
Source§impl Ansatz for TwoLocal
impl Ansatz for TwoLocal
Source§fn validate(&self) -> Result<(), CircuitError>
fn validate(&self) -> Result<(), CircuitError>
Source§fn build_circuit(&self, prefix: &str) -> Result<Circuit, CircuitError>
fn build_circuit(&self, prefix: &str) -> Result<Circuit, CircuitError>
Source§fn num_parameters(&self) -> usize
fn num_parameters(&self) -> usize
Source§fn num_qubits(&self) -> usize
fn num_qubits(&self) -> usize
Auto Trait Implementations§
impl Freeze for TwoLocal
impl RefUnwindSafe for TwoLocal
impl Send for TwoLocal
impl Sync for TwoLocal
impl Unpin for TwoLocal
impl UnsafeUnpin for TwoLocal
impl UnwindSafe for TwoLocal
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> DistributionExt for Twhere
T: ?Sized,
impl<T> DistributionExt for Twhere
T: ?Sized,
Source§impl<T> FmtForward for T
impl<T> FmtForward for T
Source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self to use its Binary implementation when Debug-formatted.Source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self to use its Display implementation when
Debug-formatted.Source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self to use its LowerExp implementation when
Debug-formatted.Source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self to use its LowerHex implementation when
Debug-formatted.Source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self to use its Octal implementation when Debug-formatted.Source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self to use its Pointer implementation when
Debug-formatted.Source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self to use its UpperExp implementation when
Debug-formatted.Source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self to use its UpperHex implementation when
Debug-formatted.impl<T, U> Imply<T> for U
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.Source§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.