Hello,
This is a simple function:
number =3
a, b, c = cirq.LineQubit.range(number)
And I want to create these variables (a, b, c) dynalically instead of writing by hand. However I know that, creating variable with this method not so good. So I wanted to ask here. What is the best way for that. Because sometimes I need to create 10 variable sometimes 3 and I do not want to write or do not want to correct each time. After creating these variable I need to save them in a list too
Don't create them as separate variables in your namespace. Instead assign the variables to a collection, and pull out the values as needed.
cirq.LineQubit.range(number)
is already returning a list. Your assignment is just unpacking it into separate variables.
Instead consider:
number = 3
all_qbits = cirq.LineQubit.range(number)
print (all_qbits)
Output:
[cirq.LineQubit(0), cirq.LineQubit(1), cirq.LineQubit(2)]
(Jan-25-2021, 10:16 PM)bowlofred Wrote: [ -> ]Don't create them as separate variables in your namespace. Instead assign the variables to a collection, and pull out the values as needed. cirq.LineQubit.range(number)
is already returning a list. Your assignment is just unpacking it into separate variables.
Instead consider:
number = 3
all_qbits = cirq.LineQubit.range(number)
print (all_qbits)
Output:
[cirq.LineQubit(0), cirq.LineQubit(1), cirq.LineQubit(2)]
Thanks it is true! But it could be nice to know logic. Because I also do following script dynalmically:
If I have 1 variable, it will be
a =kron(unitary(rz(qbt[0])))
If I have 2 vaarible, it will be
a = kron(unitary(rz(qt[0])),unitary(rz(qbt[1])))
and if I have 10, it will go...
HOw can I do it automatically?
Is that a typo and the second call should be
qbt[0]
rather than
qt[0]
? If so, we can probably write a program to index it. If it's not a typo, I don't know how you'd expect anything to understand which sequence it's using.
The problem there isn't the assignment, it's the call. You have two different calls to
kron()
. I don't know what that function does, so there might be easier ways to do it, but you could construct a list with your arguments, then pass the list into kron(). Something like:
def kron_argument_list(number):
arg_list = []
for i in range(number):
arg_list.append(unitary(rz(qbt[i])))
return arg_list
a = kron(*kron_argument_list(1))
a = kron(*kron_argument_list(2))
(Jan-25-2021, 11:09 PM)bowlofred Wrote: [ -> ]Is that a typo and the second call should be qbt[0]
rather than qt[0]
? If so, we can probably write a program to index it. If it's not a typo, I don't know how you'd expect anything to understand which sequence it's using.
The problem there isn't the assignment, it's the call. You have two different calls to kron()
. I don't know what that function does, so there might be easier ways to do it, but you could construct a list with your arguments, then pass the list into kron(). Something like:
def kron_argument_list(number):
arg_list = []
for i in range(number):
arg_list.append(unitary(rz(qbt[i])))
return arg_list
a = kron(*kron_argument_list(1))
a = kron(*kron_argument_list(2))
Thanks yes it is a typo! and if I have 1 varibale, then kron argument will include 1 variable with unitary if I have 2 variable then kron argument will include 2 argument with unitaries. I will try your method and If it is not work, I will let you know
Thanks again
Forgat to ask: a = kron(*kron_argument_list(2)) means that a = kron(*kron_argument_list(number))?? or what it means??
When I write a = kron(*kron_argument_list(2)) is this equal to write a = kron(unitary(rz(qt[0])),unitary(rz(qbt[1])))??
Yes, it should. I can't actually test it, because I don't have any of your functions or data structures.
You could call the function directly and see the argument list that it generates.
print(kron_argument_list(1))
print(kron_argument_list(2))
(Jan-25-2021, 10:47 PM)quest_ Wrote: [ -> ]If I have 2 vaarible, it will be
a = kron(unitary(rz(qt[0])),unitary(rz(qbt[1])))
and if I have 10, it will go...
HOw can I do it automatically?
The * expands lists for exactly this reason.
>>> def show(a=1, b=2, c=3):
... print(a, b, c)
...
>>> show('cat')
cat 2 3
>>> items = ['spam', 'foo']
>>> show(*items)
spam foo 3
(Jan-26-2021, 04:52 PM)nilamo Wrote: [ -> ] (Jan-25-2021, 10:47 PM)quest_ Wrote: [ -> ]If I have 2 vaarible, it will be
a = kron(unitary(rz(qt[0])),unitary(rz(qbt[1])))
and if I have 10, it will go...
HOw can I do it automatically?
The * expands lists for exactly this reason.>>> def show(a=1, b=2, c=3):
... print(a, b, c)
...
>>> show('cat')
cat 2 3
>>> items = ['spam', 'foo']
>>> show(*items)
spam foo 3
Apologize but I did not understand anyhting. Why are we seeing "spam foo 3" instead of "spam foo" when we run "show(*items)"
(Jan-25-2021, 11:40 PM)bowlofred Wrote: [ -> ]Yes, it should. I can't actually test it, because I don't have any of your functions or data structures.
You could call the function directly and see the argument list that it generates.
print(kron_argument_list(1))
print(kron_argument_list(2))
I wrote your function and when I called function like that:
a = kron(*kron_argument_list([i for i in range(qbt)]))
I have this error:
TypeError: 'tuple' object cannot be interpreted as an integer
And I have this error: TypeError: 'tuple' object cannot be interpreted as an integer, when I tried the following script
a = kron(*kron_argument_list(qbt))
(Jan-26-2021, 06:34 PM)quest_ Wrote: [ -> ] (Jan-26-2021, 04:52 PM)nilamo Wrote: [ -> ]The * expands lists for exactly this reason.>>> def show(a=1, b=2, c=3):
... print(a, b, c)
...
>>> show('cat')
cat 2 3
>>> items = ['spam', 'foo']
>>> show(*items)
spam foo 3
Apologize but I did not understand anyhting. Why are we seeing "spam foo 3" instead of "spam foo" when we run "show(*items)"
Because
show()
is always printing
a
,
b
, and
c
. The list that was expanded only had 2 elements, so only
a
and
b
had values coming from the list, while
c
still had it's default value as defined in the argument list:
def show(a=1, b=2, c=3):
Quote:a = kron(*kron_argument_list([i for i in range(qbt)]))
I have this error:
Error:
TypeError: 'tuple' object cannot be interpreted as an integer
What value does
qbt
have? I think the error is coming from
range(qbt)
, and that
qbt
is a tuple at that point.