DynamicCircuit

Enum DynamicCircuit 

Source
pub enum DynamicCircuit {
    Circuit1(Circuit<1>),
    Circuit2(Circuit<2>),
    Circuit4(Circuit<4>),
    Circuit8(Circuit<8>),
    Circuit16(Circuit<16>),
    Circuit32(Circuit<32>),
    Circuit64(Circuit<64>),
}
Expand description

Dynamic circuit representation for trait objects

Variants§

§

Circuit1(Circuit<1>)

§

Circuit2(Circuit<2>)

§

Circuit4(Circuit<4>)

§

Circuit8(Circuit<8>)

§

Circuit16(Circuit<16>)

§

Circuit32(Circuit<32>)

§

Circuit64(Circuit<64>)

Implementations§

Source§

impl DynamicCircuit

Source

pub fn from_circuit<const N: usize>(circuit: Circuit<N>) -> Result<Self>

Create from a generic circuit

Examples found in repository?
examples/tensorflow_quantum_demo.rs (line 182)
14fn main() -> Result<()> {
15    println!("=== TensorFlow Quantum Compatibility Demo ===\n");
16
17    // Step 1: Create TFQ-style quantum circuits
18    println!("1. Creating TensorFlow Quantum style circuits...");
19
20    let (circuits, circuit_symbols) = create_tfq_circuits()?;
21    println!(
22        "   - Created {} parameterized quantum circuits",
23        circuits.len()
24    );
25    println!("   - Circuit symbols: {:?}", circuit_symbols);
26
27    // Step 2: Build TFQ-style model with PQC layers
28    println!("\n2. Building TFQ-compatible model...");
29
30    let mut model = TFQModel::new(vec![4, 1]); // input_shape: [batch_size, features]
31
32    // Add quantum circuit layer (equivalent to tfq.layers.PQC)
33    // Note: QuantumCircuitLayer does not implement TFQLayer in current API
34    // model.add_layer(Box::new(QuantumCircuitLayer::new(
35    //     circuits[0].clone(),
36    //     circuit_symbols.clone(),
37    //     Observable::PauliZ(vec![0]),
38    //     Arc::new(StatevectorBackend::new(8))
39    // )));
40    println!("   - Quantum circuit layer placeholder added");
41
42    // Add classical preprocessing layer
43    // Note: TFQDenseLayer not implemented in current API
44    // model.add_layer(Box::new(TFQDenseLayer::new(
45    //     4, 8,
46    //     ActivationFunction::ReLU,
47    //     ParameterInitStrategy::XavierUniform
48    // )?));
49
50    // Add PQC layer with different observable
51    // Note: PQCLayer not implemented in current API
52    // model.add_layer(Box::new(PQCLayer::new(
53    //     circuits[1].clone(),
54    //     Observable::PauliZ(vec![1]),
55    //     RegularizationType::L2(0.01)
56    // )?));
57
58    // Add quantum convolutional layer
59    // Note: QuantumConvolutionalLayer not implemented in current API
60    // model.add_layer(Box::new(QuantumConvolutionalLayer::new(
61    //     circuits[2].clone(),
62    //     (2, 2), // kernel_size
63    //     PaddingType::Valid,
64    //     2       // stride
65    // )?));
66
67    // Final output layer
68    // Note: TFQDenseLayer not implemented in current API
69    // model.add_layer(Box::new(TFQDenseLayer::new(
70    //     8, 2,
71    //     ActivationFunction::Softmax,
72    //     ParameterInitStrategy::HeNormal
73    // )?));
74
75    println!("   Model architecture:");
76    // model.summary(); // Not implemented in current API
77
78    // Step 3: Create TFQ-style quantum dataset
79    println!("\n3. Creating TensorFlow Quantum dataset...");
80
81    let quantum_dataset = create_tfq_quantum_dataset()?;
82    // println!("   - Dataset size: {}", quantum_dataset.size());
83    // println!("   - Data encoding: {:?}", quantum_dataset.encoding_type());
84    // println!("   - Batch size: {}", quantum_dataset.batch_size());
85    println!("   - Quantum dataset created successfully");
86
87    // Step 4: Configure TFQ-style training
88    println!("\n4. Configuring TFQ training setup...");
89
90    let optimizer = TFQOptimizer::Adam {
91        learning_rate: 0.001,
92        beta1: 0.9,
93        beta2: 0.999,
94        epsilon: 1e-7,
95    };
96
97    let loss_function = TFQLossFunction::CategoricalCrossentropy;
98
99    model.compile()?;
100
101    println!("   - Optimizer: Adam");
102    println!("   - Loss: Sparse Categorical Crossentropy");
103    println!("   - Metrics: Accuracy, Precision, Recall");
104
105    // Step 5: Train with TFQ-style fit method
106    println!("\n5. Training with TensorFlow Quantum style...");
107
108    // Note: fit method not fully implemented in current API
109    // let history = model.fit(
110    //     &quantum_dataset,
111    //     15,    // epochs
112    //     0.2,   // validation_split
113    //     1,     // verbose
114    //     vec![
115    //         Box::new(EarlyStoppingCallback::new(3, "val_loss")),      // patience, monitor
116    //         Box::new(ReduceLROnPlateauCallback::new(0.5, 2)),         // factor, patience
117    //     ]
118    // )?;
119    println!("   Training setup configured (fit method placeholder)");
120
121    // println!("   Training completed!");
122    // println!("   - Final training accuracy: {:.3}", history.final_metric("accuracy"));
123    // println!("   - Final validation accuracy: {:.3}", history.final_metric("val_accuracy"));
124    // println!("   - Best epoch: {}", history.best_epoch());
125    println!("   Training placeholder completed");
126
127    // Step 6: Evaluate model performance
128    println!("\n6. Model evaluation...");
129
130    let test_dataset = create_tfq_test_dataset()?;
131    // let evaluation_results = model.evaluate(&test_dataset, 1)?;  // verbose
132    //
133    // println!("   Test Results:");
134    // for (metric, value) in evaluation_results.iter() {
135    //     println!("   - {}: {:.4}", metric, value);
136    // }
137    println!("   Test dataset created successfully");
138
139    // Step 7: Quantum circuit analysis
140    println!("\n7. Quantum circuit analysis...");
141
142    // let circuit_analysis = model.analyze_quantum_circuits()?;
143    // println!("   Circuit Properties:");
144    // println!("   - Total quantum parameters: {}", circuit_analysis.total_quantum_params);
145    // println!("   - Circuit depth: {}", circuit_analysis.max_circuit_depth);
146    // println!("   - Gate types used: {:?}", circuit_analysis.gate_types);
147    // println!("   - Entangling gates: {}", circuit_analysis.entangling_gate_count);
148    println!("   Circuit analysis placeholder completed");
149
150    // Step 8: Parameter shift gradients (TFQ-style)
151    println!("\n8. Computing parameter shift gradients...");
152
153    // let sample_input = quantum_dataset.get_batch(0)?;
154    // let gradients = model.compute_parameter_shift_gradients(&sample_input)?;
155    println!("   Parameter shift gradients placeholder");
156
157    // println!("   Gradient Analysis:");
158    // println!("   - Quantum gradients computed: {}", gradients.quantum_gradients.len());
159    // println!("   - Classical gradients computed: {}", gradients.classical_gradients.len());
160    // println!("   - Max quantum gradient: {:.6}",
161    //     gradients.quantum_gradients.iter().fold(0.0f64, |a, &b| a.max(b.abs())));
162    // println!("   - Gradient variance: {:.6}",
163    //     compute_gradient_variance(&gradients.quantum_gradients));
164    println!("   Gradient analysis placeholder completed");
165
166    // Step 9: Quantum expectation values
167    println!("\n9. Computing quantum expectation values...");
168
169    let observables = vec![Observable::PauliZ(vec![0]), Observable::PauliZ(vec![1])];
170
171    // let expectation_values = model.compute_expectation_values(&sample_input, &observables)?;
172    // println!("   Expectation Values:");
173    // for (i, (obs, val)) in observables.iter().zip(expectation_values.iter()).enumerate() {
174    //     println!("   - Observable {}: {:.4}", i, val);
175    // }
176    println!("   Expectation values placeholder completed");
177
178    // Step 10: TFQ utils demonstrations
179    println!("\n10. TensorFlow Quantum utilities...");
180
181    // Circuit conversion
182    let dynamic_circuit = DynamicCircuit::from_circuit(circuits[0].clone())?;
183    let tfq_format_circuit = tfq_utils::circuit_to_tfq_format(&dynamic_circuit)?;
184    println!("    - Converted circuit to TFQ format (placeholder)");
185
186    // Batch circuit execution
187    // let batch_circuits = vec![circuits[0].clone(), circuits[1].clone()];
188    // let batch_params = Array2::from_shape_fn((2, 4), |(i, j)| (i + j) as f64 * 0.1);
189    // let batch_results = tfq_utils::batch_execute_circuits(&batch_circuits, &batch_params, &observables, &backend)?;
190    // println!("    - Batch execution results shape: {:?}", batch_results.dim());
191    println!("    - Batch execution placeholder completed");
192
193    // Data encoding utilities
194    let classical_data = Array2::from_shape_fn((10, 4), |(i, j)| (i + j) as f64 * 0.2);
195    // let encoded_circuits = tfq_utils::encode_data_to_circuits(
196    //     &classical_data,
197    //     DataEncodingType::Angle
198    // )?;
199    let encoded_circuits = vec![tfq_utils::create_data_encoding_circuit(
200        4,
201        DataEncodingType::Angle,
202    )?];
203    println!(
204        "    - Encoded {} data points to quantum circuits",
205        encoded_circuits.len()
206    );
207
208    // Step 11: Compare with TensorFlow classical model
209    println!("\n11. Comparing with TensorFlow classical equivalent...");
210
211    let classical_model = create_tensorflow_classical_model()?;
212    // let classical_accuracy = train_classical_tensorflow_model(classical_model, &quantum_dataset)?;
213    //
214    // let quantum_accuracy = evaluation_results.get("accuracy").unwrap_or(&0.0);
215    // println!("    - Quantum TFQ model accuracy: {:.3}", quantum_accuracy);
216    // println!("    - Classical TF model accuracy: {:.3}", classical_accuracy);
217    // println!("    - Quantum advantage: {:.3}", quantum_accuracy - classical_accuracy);
218    println!("    - Classical comparison placeholder completed");
219
220    // Step 12: Model export (TFQ format)
221    println!("\n12. Exporting model in TFQ format...");
222
223    // model.save_tfq_format("quantum_model_tfq.pb")?;
224    // println!("    - Model exported to: quantum_model_tfq.pb");
225    //
226    // // Export to TensorFlow SavedModel format
227    // model.export_savedmodel("quantum_model_savedmodel/")?;
228    // println!("    - SavedModel exported to: quantum_model_savedmodel/");
229    println!("    - Model export placeholder completed");
230
231    // Step 13: Advanced TFQ features
232    println!("\n13. Advanced TensorFlow Quantum features...");
233
234    // Quantum data augmentation
235    // let augmented_dataset = quantum_dataset.augment_with_noise(0.05)?;
236    // println!("    - Created augmented dataset with noise level 0.05");
237    //
238    // // Circuit optimization for hardware
239    // let optimized_circuits = tfq_utils::optimize_circuits_for_hardware(
240    //     &circuits,
241    //     HardwareType::IonQ
242    // )?;
243    // println!("    - Optimized {} circuits for IonQ hardware", optimized_circuits.len());
244    //
245    // // Barren plateau analysis
246    // let plateau_analysis = analyze_barren_plateaus(&model, &quantum_dataset)?;
247    // println!("    - Barren plateau risk: {:.3}", plateau_analysis.risk_score);
248    // println!("    - Recommended mitigation: {}", plateau_analysis.mitigation_strategy);
249    println!("    - Advanced features placeholder completed");
250
251    println!("\n=== TensorFlow Quantum Demo Complete ===");
252
253    Ok(())
254}
Source

pub fn num_qubits(&self) -> usize

Get the number of qubits

Source

pub fn num_gates(&self) -> usize

Get the number of gates (placeholder implementation)

Source

pub fn depth(&self) -> usize

Get circuit depth (placeholder implementation)

Source

pub fn gates(&self) -> Vec<&dyn GateOp>

Get gates (placeholder implementation)

Trait Implementations§

Source§

impl Clone for DynamicCircuit

Source§

fn clone(&self) -> DynamicCircuit

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for DynamicCircuit

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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,