Optimal quantum repeaters for qubits and qudits