TensorNetwork

Struct TensorNetwork 

Source
pub struct TensorNetwork { /* private fields */ }
Expand description

Tensor network representation of a quantum circuit

Implementations§

Source§

impl TensorNetwork

Source

pub fn new() -> Self

Create a new empty tensor network

Examples found in repository?
examples/tensor_network_demo.rs (line 50)
25fn demo_basic_tensor_network() -> quantrs2_core::error::QuantRS2Result<()> {
26    println!("--- Basic Tensor Network Construction ---");
27
28    // Create simple tensors
29    let identity = Tensor::identity(2, "in".to_string(), "out".to_string());
30    println!(
31        "Created identity tensor: rank={}, size={}",
32        identity.rank(),
33        identity.size()
34    );
35
36    // Create Hadamard tensor
37    let h_data = vec![
38        C64::new(1.0 / 2.0_f64.sqrt(), 0.0),
39        C64::new(1.0 / 2.0_f64.sqrt(), 0.0),
40        C64::new(1.0 / 2.0_f64.sqrt(), 0.0),
41        C64::new(-1.0 / 2.0_f64.sqrt(), 0.0),
42    ];
43    let h_tensor = Tensor::new(
44        h_data,
45        vec![2, 2],
46        vec!["h_in".to_string(), "h_out".to_string()],
47    );
48
49    // Build tensor network
50    let mut tn = TensorNetwork::new();
51    let id_idx = tn.add_tensor(identity);
52    let h_idx = tn.add_tensor(h_tensor);
53
54    // Connect tensors
55    tn.add_bond(id_idx, "out".to_string(), h_idx, "h_in".to_string())?;
56
57    println!("Built tensor network with {} tensors and {} bonds", 2, 1);
58    println!();
59
60    Ok(())
61}
Source

pub fn add_tensor(&mut self, tensor: Tensor) -> usize

Add a tensor to the network

Examples found in repository?
examples/tensor_network_demo.rs (line 51)
25fn demo_basic_tensor_network() -> quantrs2_core::error::QuantRS2Result<()> {
26    println!("--- Basic Tensor Network Construction ---");
27
28    // Create simple tensors
29    let identity = Tensor::identity(2, "in".to_string(), "out".to_string());
30    println!(
31        "Created identity tensor: rank={}, size={}",
32        identity.rank(),
33        identity.size()
34    );
35
36    // Create Hadamard tensor
37    let h_data = vec![
38        C64::new(1.0 / 2.0_f64.sqrt(), 0.0),
39        C64::new(1.0 / 2.0_f64.sqrt(), 0.0),
40        C64::new(1.0 / 2.0_f64.sqrt(), 0.0),
41        C64::new(-1.0 / 2.0_f64.sqrt(), 0.0),
42    ];
43    let h_tensor = Tensor::new(
44        h_data,
45        vec![2, 2],
46        vec!["h_in".to_string(), "h_out".to_string()],
47    );
48
49    // Build tensor network
50    let mut tn = TensorNetwork::new();
51    let id_idx = tn.add_tensor(identity);
52    let h_idx = tn.add_tensor(h_tensor);
53
54    // Connect tensors
55    tn.add_bond(id_idx, "out".to_string(), h_idx, "h_in".to_string())?;
56
57    println!("Built tensor network with {} tensors and {} bonds", 2, 1);
58    println!();
59
60    Ok(())
61}
Source

pub fn add_bond( &mut self, t1: usize, idx1: String, t2: usize, idx2: String, ) -> QuantRS2Result<()>

Connect two tensor indices

Examples found in repository?
examples/tensor_network_demo.rs (line 55)
25fn demo_basic_tensor_network() -> quantrs2_core::error::QuantRS2Result<()> {
26    println!("--- Basic Tensor Network Construction ---");
27
28    // Create simple tensors
29    let identity = Tensor::identity(2, "in".to_string(), "out".to_string());
30    println!(
31        "Created identity tensor: rank={}, size={}",
32        identity.rank(),
33        identity.size()
34    );
35
36    // Create Hadamard tensor
37    let h_data = vec![
38        C64::new(1.0 / 2.0_f64.sqrt(), 0.0),
39        C64::new(1.0 / 2.0_f64.sqrt(), 0.0),
40        C64::new(1.0 / 2.0_f64.sqrt(), 0.0),
41        C64::new(-1.0 / 2.0_f64.sqrt(), 0.0),
42    ];
43    let h_tensor = Tensor::new(
44        h_data,
45        vec![2, 2],
46        vec!["h_in".to_string(), "h_out".to_string()],
47    );
48
49    // Build tensor network
50    let mut tn = TensorNetwork::new();
51    let id_idx = tn.add_tensor(identity);
52    let h_idx = tn.add_tensor(h_tensor);
53
54    // Connect tensors
55    tn.add_bond(id_idx, "out".to_string(), h_idx, "h_in".to_string())?;
56
57    println!("Built tensor network with {} tensors and {} bonds", 2, 1);
58    println!();
59
60    Ok(())
61}
Source

pub fn contract_all(&self) -> QuantRS2Result<Tensor>

Contract the entire network to a single tensor

Examples found in repository?
examples/tensor_network_demo.rs (line 237)
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}
Source

pub fn compress( &mut self, max_bond_dim: usize, tolerance: f64, ) -> QuantRS2Result<()>

Apply SVD-based compression

Trait Implementations§

Source§

impl Debug for TensorNetwork

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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 more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

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

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> Ungil for T
where T: Send,