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 can make you using the trace distance as the loss function.

Parameters:
target_state : State

The target state to be used to compute the trace distance.

from quairkit.database import one_state, bell_state, random_state

# 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)  # Output: tensor(0.7071)

# Batched states example
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)  # Output: tensor([0.7912, 0.7283])
Single state distance: tensor(0.7071)
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.

Parameters:
state : State

The input state which will be used to compute the trace distance with the target state.

Raises:

NotImplementedError – The backend is wrong or not supported.

Returns:

The trace distance between the input state and the target state.

Return type:

Tensor

class quairkit.loss.distance.StateFidelity(target_state)

The class of the loss function to compute the state fidelity.

This interface can make you using the state fidelity as the loss function.

Parameters:
target_state : State

The target state to be used to compute the state fidelity.

    from quairkit.database import one_state, bell_state, random_state

    # 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)  # Output: tensor(0.7071)

    # Batched states example
    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)  # Output: tensor([0.5658, 0.7090])

::

    Single state fidelity: tensor(0.7071)
    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.

Parameters:
state : State

The input state which will be used to compute the state fidelity with the target state.

Raises:

NotImplementedError – The backend is wrong or not supported.

Returns:

The state fidelity between the input state and the target state.

Return type:

Tensor