# Buggy code
def reps(self, repetitions: int) -> None:
    """Set the repetitions.

    Args:
        repetitions: The new repetitions.
    """
    if repetitions != self._reps:
        self._invalidate()
        self._reps = repetitions


# Traceback (UnboundLocalError)
---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
<ipython-input-7-664a076be942> in <module>
----> 1 print(NLocal(1, reps=0))

~/envs/qopt/lib/python3.7/site-packages/qiskit/circuit/library/n_local/n_local.py in __str__(self)
920                        'cswap', 'ccx', 'cu1', 'cu3', 'u1', 'u2', 'u3']
921         return transpile(self, basis_gates=basis_gates,
--> 922                          optimization_level=0).draw(output='text').single_string()
923
924

~/envs/qopt/lib/python3.7/site-packages/qiskit/compiler/transpile.py in transpile(circuits, backend, basis_gates, coupling_map, backend_properties, initial_layout, layout_method, routing_method, translation_method, seed_transpiler, optimization_level, pass_manager, callback, output_name)
214
215     # Transpile circuits in parallel
--> 216     circuits = parallel_map(_transpile_circuit, list(zip(circuits, transpile_args)))
217
218     if len(circuits) == 1:

~/envs/qopt/lib/python3.7/site-packages/qiskit/tools/parallel.py in parallel_map(task, values, task_args, task_kwargs, num_processes)
106         return []
107     if len(values) == 1:
--> 108         return [task(values[0], *task_args, **task_kwargs)]
109
110     Publisher().publish("terra.parallel.start", len(values))

~/envs/qopt/lib/python3.7/site-packages/qiskit/compiler/transpile.py in _transpile_circuit(circuit_config_tuple)
315
316     result = pass_manager.run(circuit, callback=transpile_config['callback'],
--> 317                               output_name=transpile_config['output_name'])
318
319     if transpile_config['faulty_qubits_map']:

~/envs/qopt/lib/python3.7/site-packages/qiskit/transpiler/passmanager.py in run(self, circuits, output_name, callback)
212         """
213         if isinstance(circuits, QuantumCircuit):
--> 214             return self._run_single_circuit(circuits, output_name, callback)
215         elif len(circuits) == 1:
216             return self._run_single_circuit(circuits[0], output_name, callback)

~/envs/qopt/lib/python3.7/site-packages/qiskit/transpiler/passmanager.py in _run_single_circuit(self, circuit, output_name, callback)
275         if callback is None and self.callback:  # TODO to remove with __init__(callback)
276             callback = self.callback
--> 277         result = running_passmanager.run(circuit, output_name=output_name, callback=callback)
278         self.property_set = running_passmanager.property_set
279         return result

~/envs/qopt/lib/python3.7/site-packages/qiskit/transpiler/runningpassmanager.py in run(self, circuit, output_name, callback)
105         """
106         name = circuit.name
--> 107         dag = circuit_to_dag(circuit)
108         del circuit
109

~/envs/qopt/lib/python3.7/site-packages/qiskit/converters/circuit_to_dag.py in circuit_to_dag(circuit)
55         dagcircuit.add_creg(register)
56
---> 57     for instruction, qargs, cargs in circuit.data:
58         dagcircuit.apply_operation_back(instruction.copy(), qargs, cargs)
59     return dagcircuit

~/envs/qopt/lib/python3.7/site-packages/qiskit/circuit/library/blueprintcircuit.py in data(self)
90     def data(self):
91         if self._data is None:
---> 92             self._build()
93         return super().data
94

~/envs/qopt/lib/python3.7/site-packages/qiskit/circuit/library/n_local/n_local.py in _build(self)
899             if self.insert_barriers:
900                 self.barrier()
--> 901             self._build_rotation_layer(param_iter, i)
902
903         # add the appended layers

UnboundLocalError: local variable 'i' referenced before assignment

# Fixed code
def reps(self, repetitions: int) -> None:
    """Set the repetitions.

    Args:
        repetitions: The new repetitions.

    Raises:
        ValueError: If reps setter has parameter repetitions <= 0.
    """
    if repetitions <= 0:
        raise ValueError("The repetitions should be larger than or equal to 1")
    if repetitions != self._reps:
        self._invalidate()
        self._reps = repetitions