pub struct CircuitToTensorNetwork<const N: usize> { /* private fields */ }
Expand description
Convert a quantum circuit to tensor network representation
Implementations§
Source§impl<const N: usize> CircuitToTensorNetwork<N>
impl<const N: usize> CircuitToTensorNetwork<N>
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new converter
Examples found in repository?
examples/tensor_network_demo.rs (line 227)
199fn demo_tensor_contraction() -> quantrs2_core::error::QuantRS2Result<()> {
200 println!("--- Tensor Contraction Optimization ---");
201
202 // Create a circuit with specific structure
203 let mut circuit = Circuit::<4>::new();
204
205 // Layer 1: Single-qubit gates
206 for i in 0..4 {
207 circuit.add_gate(Hadamard { target: QubitId(i) })?;
208 }
209
210 // Layer 2: Entangling gates
211 circuit.add_gate(CNOT {
212 control: QubitId(0),
213 target: QubitId(1),
214 })?;
215 circuit.add_gate(CNOT {
216 control: QubitId(2),
217 target: QubitId(3),
218 })?;
219
220 // Layer 3: Cross entangling
221 circuit.add_gate(CNOT {
222 control: QubitId(1),
223 target: QubitId(2),
224 })?;
225
226 // Convert to tensor network
227 let converter = CircuitToTensorNetwork::<4>::new()
228 .with_max_bond_dim(8)
229 .with_tolerance(1e-12);
230
231 let tn = converter.convert(&circuit)?;
232
233 println!("Converted circuit to tensor network");
234 println!("Network has {} tensors", circuit.num_gates());
235
236 // Contract the network
237 let result = tn.contract_all()?;
238 println!("Contracted to single tensor of rank {}", result.rank());
239
240 println!();
241 Ok(())
242}
Sourcepub fn with_max_bond_dim(self, dim: usize) -> Self
pub fn with_max_bond_dim(self, dim: usize) -> Self
Set maximum bond dimension
Examples found in repository?
examples/tensor_network_demo.rs (line 228)
199fn demo_tensor_contraction() -> quantrs2_core::error::QuantRS2Result<()> {
200 println!("--- Tensor Contraction Optimization ---");
201
202 // Create a circuit with specific structure
203 let mut circuit = Circuit::<4>::new();
204
205 // Layer 1: Single-qubit gates
206 for i in 0..4 {
207 circuit.add_gate(Hadamard { target: QubitId(i) })?;
208 }
209
210 // Layer 2: Entangling gates
211 circuit.add_gate(CNOT {
212 control: QubitId(0),
213 target: QubitId(1),
214 })?;
215 circuit.add_gate(CNOT {
216 control: QubitId(2),
217 target: QubitId(3),
218 })?;
219
220 // Layer 3: Cross entangling
221 circuit.add_gate(CNOT {
222 control: QubitId(1),
223 target: QubitId(2),
224 })?;
225
226 // Convert to tensor network
227 let converter = CircuitToTensorNetwork::<4>::new()
228 .with_max_bond_dim(8)
229 .with_tolerance(1e-12);
230
231 let tn = converter.convert(&circuit)?;
232
233 println!("Converted circuit to tensor network");
234 println!("Network has {} tensors", circuit.num_gates());
235
236 // Contract the network
237 let result = tn.contract_all()?;
238 println!("Contracted to single tensor of rank {}", result.rank());
239
240 println!();
241 Ok(())
242}
Sourcepub fn with_tolerance(self, tol: f64) -> Self
pub fn with_tolerance(self, tol: f64) -> Self
Set truncation tolerance
Examples found in repository?
examples/tensor_network_demo.rs (line 229)
199fn demo_tensor_contraction() -> quantrs2_core::error::QuantRS2Result<()> {
200 println!("--- Tensor Contraction Optimization ---");
201
202 // Create a circuit with specific structure
203 let mut circuit = Circuit::<4>::new();
204
205 // Layer 1: Single-qubit gates
206 for i in 0..4 {
207 circuit.add_gate(Hadamard { target: QubitId(i) })?;
208 }
209
210 // Layer 2: Entangling gates
211 circuit.add_gate(CNOT {
212 control: QubitId(0),
213 target: QubitId(1),
214 })?;
215 circuit.add_gate(CNOT {
216 control: QubitId(2),
217 target: QubitId(3),
218 })?;
219
220 // Layer 3: Cross entangling
221 circuit.add_gate(CNOT {
222 control: QubitId(1),
223 target: QubitId(2),
224 })?;
225
226 // Convert to tensor network
227 let converter = CircuitToTensorNetwork::<4>::new()
228 .with_max_bond_dim(8)
229 .with_tolerance(1e-12);
230
231 let tn = converter.convert(&circuit)?;
232
233 println!("Converted circuit to tensor network");
234 println!("Network has {} tensors", circuit.num_gates());
235
236 // Contract the network
237 let result = tn.contract_all()?;
238 println!("Contracted to single tensor of rank {}", result.rank());
239
240 println!();
241 Ok(())
242}
Sourcepub fn convert(&self, circuit: &Circuit<N>) -> QuantRS2Result<TensorNetwork>
pub fn convert(&self, circuit: &Circuit<N>) -> QuantRS2Result<TensorNetwork>
Convert circuit to tensor network
Examples found in repository?
examples/tensor_network_demo.rs (line 231)
199fn demo_tensor_contraction() -> quantrs2_core::error::QuantRS2Result<()> {
200 println!("--- Tensor Contraction Optimization ---");
201
202 // Create a circuit with specific structure
203 let mut circuit = Circuit::<4>::new();
204
205 // Layer 1: Single-qubit gates
206 for i in 0..4 {
207 circuit.add_gate(Hadamard { target: QubitId(i) })?;
208 }
209
210 // Layer 2: Entangling gates
211 circuit.add_gate(CNOT {
212 control: QubitId(0),
213 target: QubitId(1),
214 })?;
215 circuit.add_gate(CNOT {
216 control: QubitId(2),
217 target: QubitId(3),
218 })?;
219
220 // Layer 3: Cross entangling
221 circuit.add_gate(CNOT {
222 control: QubitId(1),
223 target: QubitId(2),
224 })?;
225
226 // Convert to tensor network
227 let converter = CircuitToTensorNetwork::<4>::new()
228 .with_max_bond_dim(8)
229 .with_tolerance(1e-12);
230
231 let tn = converter.convert(&circuit)?;
232
233 println!("Converted circuit to tensor network");
234 println!("Network has {} tensors", circuit.num_gates());
235
236 // Contract the network
237 let result = tn.contract_all()?;
238 println!("Contracted to single tensor of rank {}", result.rank());
239
240 println!();
241 Ok(())
242}
Auto Trait Implementations§
impl<const N: usize> Freeze for CircuitToTensorNetwork<N>
impl<const N: usize> RefUnwindSafe for CircuitToTensorNetwork<N>
impl<const N: usize> Send for CircuitToTensorNetwork<N>
impl<const N: usize> Sync for CircuitToTensorNetwork<N>
impl<const N: usize> Unpin for CircuitToTensorNetwork<N>
impl<const N: usize> UnwindSafe for CircuitToTensorNetwork<N>
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
Mutably borrows from an owned value. Read more
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>
Converts
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>
Converts
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> Pointable for T
impl<T> Pointable for T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self
from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
self
is actually part of its subset T
(and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
Use with care! Same as
self.to_subset
but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self
to the equivalent element of its superset.