def cost_function(theta, state, circuit, shots=1000):
    """Cost function encoding the difference between a state and its up-to-phases canonical form
        Args:
            theta (array): parameters of the unitary rotations.
            state (cplx array): three-qubit random state.
            circuit (models.Circuit): Qibo variational circuit.
            shots (int): Shots used for measuring every circuit.
        Returns:
            float, cost function
    """
    circuit.set_parameters(theta)
    measurements = circuit(state, nshots=shots).frequencies(binary=False)
    return (measurements[1] + measurements[2] + measurements[3]) / shots