in src/braket/circuits/circuit.py [0:0]
def add(self, addable: AddableTypes, *args, **kwargs) -> Circuit:
"""
Generic add method for adding item(s) to self. Any arguments that
`add_circuit()` and / or `add_instruction()` and / or `add_result_type`
supports are supported by this method. If adding a
subroutine, check with that subroutines documentation to determine what
input it allows.
Args:
addable (AddableTypes): The item(s) to add to self. Default = `None`.
*args: Variable length argument list.
**kwargs: Arbitrary keyword arguments.
Returns:
Circuit: self
Raises:
TypeError: If `addable` is an unsupported type
See Also:
`add_circuit()`
`add_instruction()`
`add_result_type()`
Examples:
>>> circ = Circuit().add([Instruction(Gate.H(), 4), Instruction(Gate.CNot(), [4, 5])])
>>> circ = Circuit().add([ResultType.StateVector()])
>>> circ = Circuit().h(4).cnot([4, 5])
>>> @circuit.subroutine()
>>> def bell_pair(target):
... return Circuit().h(target[0]).cnot(target[0: 2])
...
>>> circ = Circuit().add(bell_pair, [4,5])
"""
def _flatten(addable):
if isinstance(addable, Iterable):
for item in addable:
yield from _flatten(item)
else:
yield addable
for item in _flatten(addable):
if isinstance(item, Instruction):
self.add_instruction(item, *args, **kwargs)
elif isinstance(item, ResultType):
self.add_result_type(item, *args, **kwargs)
elif isinstance(item, Circuit):
self.add_circuit(item, *args, **kwargs)
elif callable(item):
self.add(item(*args, **kwargs))
else:
raise TypeError(f"Cannot add a '{type(item)}' to a Circuit")
return self