quairkit.loss.distance¶
The source file of the class for the distance.
- class quairkit.loss.distance.TraceDistance(target_state)¶
The class of the loss function to compute the trace distance.
This interface allows you to use the trace distance as a loss function for training quantum circuits. The trace distance is defined as \(D(\rho, \sigma) = \frac{1}{2}\text{tr}|\rho-\sigma|\).
- Parameters:¶
- target_state : StateSimulator¶
The target quantum state to be used to compute the trace distance. Can be a single state or a batch of states.
Note
This class supports batch operations. When both target and input states have batch dimensions, the trace distance is computed element-wise along the batch dimension.
Examples
from quairkit.database import one_state, bell_state from quairkit.loss import TraceDistance # Single state example target_state = one_state(1) # Define target input_state = bell_state(1) # Define input trace_distance = TraceDistance(target_state) result = trace_distance(input_state) print('Single state distance:', result)Single state distance: tensor(0.7071)# Batched states example from quairkit.database import random_state target_state_batch = random_state(num_systems=1, size=2) # Batch of 2 targets input_state_batch = random_state(num_systems=1, size=2) # Batch of 2 inputs trace_distance = TraceDistance(target_state_batch) batch_result = trace_distance(input_state_batch) print('Batched distances:', batch_result)Batched distances: tensor([0.7912, 0.7283])- forward(state)¶
Compute the trace distance between the input state and the target state.
The value computed by this function can be used as a loss function to optimize quantum circuits.
- Parameters:¶
- state : StateSimulator¶
The input quantum state which will be used to compute the trace distance with the target state. Can be a single state or a batch of states. Must have the same number of systems as the target state.
- Returns:¶
The trace distance between the input state and the target state. Returns a scalar for single states, or a tensor for batched states.
- Raises:¶
AssertionError – If the number of systems in the input state does not match the target state.
NotImplementedError – If the backend is wrong or not supported.
- Return type:¶
Tensor
- class quairkit.loss.distance.StateFidelity(target_state)¶
The class of the loss function to compute the state fidelity.
This interface allows you to use the state fidelity as a loss function for training quantum circuits. The state fidelity is defined as \(F(\rho, \sigma) = \text{tr}(\sqrt{\sqrt{\rho}\sigma\sqrt{\rho}})\).
- Parameters:¶
- target_state : StateSimulator¶
The target quantum state to be used to compute the state fidelity. Can be a single state or a batch of states.
Note
This class supports batch operations. When both target and input states have batch dimensions, the fidelity is computed element-wise along the batch dimension.
Examples
from quairkit.database import one_state, bell_state from quairkit.loss import StateFidelity # Single state example target_state = one_state(1) # Define target input_state = bell_state(1) # Define input fidelity_calculator = StateFidelity(target_state) result = fidelity_calculator(input_state) print('Single state fidelity:', result)Single state fidelity: tensor(0.7071)# Batched states example from quairkit.database import random_state target_batch = random_state(num_systems=1, size=2) # Batch of 2 targets input_batch = random_state(num_systems=1, size=2) # Batch of 2 inputs fidelity_calculator = StateFidelity(target_batch) batch_result = fidelity_calculator(input_batch) print('Batched fidelities:', batch_result)Batched fidelities: tensor([0.5658, 0.7090])- forward(state)¶
Compute the state fidelity between the input state and the target state.
The value computed by this function can be used as a loss function to optimize quantum circuits.
- Parameters:¶
- state : StateSimulator¶
The input quantum state which will be used to compute the state fidelity with the target state. Can be a single state or a batch of states. Must have the same number of systems as the target state.
- Returns:¶
The state fidelity between the input state and the target state. Returns a scalar for single states, or a tensor for batched states. The fidelity ranges from 0 to 1.
- Raises:¶
AssertionError – If the number of systems in the input state does not match the target state.
NotImplementedError – If the backend is wrong or not supported.
- Return type:¶
Tensor