Gradients

QuantumFlow: Gradients of parameterized gates, and gradient descent optimizers

quantumflow.expectation_gradients(ket0: State, circ: Circuit, hermitian: Operation, dfunc: Callable[[float], float] | None = None) Sequence[float]

Calculate the gradients of a function of expectation for a parameterized quantum circuit, using the middle-out algorithm.

Parameters:
  • ket0 – An initial state.

  • circ – A circuit that acts on the initial state.

  • hermitian – A Hermitian Operation for which the expectation is evaluated

  • dfunc – Derivative of func. Defaults to identity.

Returns:

The gradient with respect to the circuits parameters.

quantumflow.state_fidelity_gradients(ket0: State, ket1: State, circ: Circuit) Sequence[float]

Calculate the gradients of state fidelity for a parameterized quantum circuit, using the middle-out algorithm.

Parameters:
  • ket0 – An initial state.

  • ket1 – A target state. We calculate the fidelity between this state and

  • circuit. (the resultant of the) –

  • circ – A circuit that acts on ket0.

Returns:

The gradients of state fidelity with respect to the circuits parameters.

quantumflow.state_angle_gradients(ket0: State, ket1: State, circ: Circuit) Sequence[float]

Calculate the gradients of state angle for a parameterized quantum circuit, using the middle-out algorithm.

Parameters:
  • ket0 – An initial state.

  • ket1 – A target state. We calculate the fidelity between this state and the resultant of the circuit.

  • circ – A circuit that acts on ket0.

Returns:

The gradients of the inter-state angle with respect to the circuits parameters.

quantumflow.parameter_shift_circuits(circ: Circuit, index: int) Tuple[float, Circuit, Circuit]

Calculate the gradients of state angle for a parameterized quantum circuit, using the parameter-shift rule.

Returns the gate shift-constant, and two circuits, circ0, circ1. Gradients are proportional to the difference in expectation between the two circuits.

r, circ0, circ1 = qf.parameter_shift_circuits(circ, n)
fid0 = qf.state_fidelity(circ0.run(ket0), ket1)
fid1 = qf.state_fidelity(circ1.run(ket0), ket1)
grad = r*(fid1-fid0)
Parameters:
  • circ – A quantum circuit with parameterized gates

  • index – The index of the target gate in the quantum circuit

Returns:

r – The gate shift constant circ0: Circuit with parameter shift on target gate. circ1: Circuit with negative parameter shift on target gate.