mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
Approve functions used as default arguments (#4699)
* Approve functions used as default argumenets * The default value for **seed** is the result of a function call The default value for **seed** is the result of a function call which is not normally recommended and causes flake8-bugbear to raise a B008 error. However, in this case, it is accptable because `LinearCongruentialGenerator.__init__()` will only be called once per instance and it ensures that each instance will generate a unique sequence of numbers. * The default value for **backend** is the result of a function call The default value for **backend** is the result of a function call which is not normally recommended and causes flake8-bugbear to raise a B008 error. However, in this case, it is accptable because `Aer.get_backend()` is called when the function is definition and that same backend is then reused for function calls. * Update linear_congruential_generator.py * Update ripple_adder_classic.py * Update ripple_adder_classic.py * Update ripple_adder_classic.py * Update ripple_adder_classic.py * Update ripple_adder_classic.py
This commit is contained in:
parent
097f830238
commit
ef9827166e
@ -8,7 +8,13 @@ class LinearCongruentialGenerator:
|
|||||||
A pseudorandom number generator.
|
A pseudorandom number generator.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, multiplier, increment, modulo, seed=int(time())):
|
# The default value for **seed** is the result of a function call which is not
|
||||||
|
# normally recommended and causes flake8-bugbear to raise a B008 error. However,
|
||||||
|
# in this case, it is accptable because `LinearCongruentialGenerator.__init__()`
|
||||||
|
# will only be called once per instance and it ensures that each instance will
|
||||||
|
# generate a unique sequence of numbers.
|
||||||
|
|
||||||
|
def __init__(self, multiplier, increment, modulo, seed=int(time())): # noqa: B008
|
||||||
"""
|
"""
|
||||||
These parameters are saved and used when nextNumber() is called.
|
These parameters are saved and used when nextNumber() is called.
|
||||||
|
|
||||||
|
@ -53,8 +53,16 @@ def full_adder(
|
|||||||
circuit.cx(input1_loc, input2_loc)
|
circuit.cx(input1_loc, input2_loc)
|
||||||
|
|
||||||
|
|
||||||
|
# The default value for **backend** is the result of a function call which is not
|
||||||
|
# normally recommended and causes flake8-bugbear to raise a B008 error. However,
|
||||||
|
# in this case, this is accptable because `Aer.get_backend()` is called when the
|
||||||
|
# function is defined and that same backend is then reused for all function calls.
|
||||||
|
|
||||||
|
|
||||||
def ripple_adder(
|
def ripple_adder(
|
||||||
val1: int, val2: int, backend: BaseBackend = Aer.get_backend("qasm_simulator")
|
val1: int,
|
||||||
|
val2: int,
|
||||||
|
backend: BaseBackend = Aer.get_backend("qasm_simulator"), # noqa: B008
|
||||||
) -> int:
|
) -> int:
|
||||||
"""
|
"""
|
||||||
Quantum Equivalent of a Ripple Adder Circuit
|
Quantum Equivalent of a Ripple Adder Circuit
|
||||||
@ -63,7 +71,7 @@ def ripple_adder(
|
|||||||
Currently only adds 'emulated' Classical Bits
|
Currently only adds 'emulated' Classical Bits
|
||||||
but nothing prevents us from doing this with hadamard'd bits :)
|
but nothing prevents us from doing this with hadamard'd bits :)
|
||||||
|
|
||||||
Only supports adding +ve Integers
|
Only supports adding positive integers
|
||||||
|
|
||||||
>>> ripple_adder(3, 4)
|
>>> ripple_adder(3, 4)
|
||||||
7
|
7
|
||||||
@ -99,7 +107,7 @@ def ripple_adder(
|
|||||||
res = execute(circuit, backend, shots=1).result()
|
res = execute(circuit, backend, shots=1).result()
|
||||||
|
|
||||||
# The result is in binary. Convert it back to int
|
# The result is in binary. Convert it back to int
|
||||||
return int(list(res.get_counts().keys())[0], 2)
|
return int(list(res.get_counts())[0], 2)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
Loading…
Reference in New Issue
Block a user