3
$\begingroup$

How to create a variable qbit QuantumCirciut like this ? I need to put v list on QuantumCircuit() method

n = 4 #number of vector to encode
v = list() #list of register qbits to encode 4 vectors
a = QuantumRegister(1,"a") #ancilla qbit
for i in range(n):
   v.append(QuantumRegister(2,"v"+str(i))) #creates n QuantumRegister
b = ClassicalRegister(1,"b") #classical bit
c = QuantumCircuit(a,?,b)
$\endgroup$

2 Answers 2

1
$\begingroup$

You can use QuantumCircuit.add_register() to add a new register (classical or quantum) whenever there is a need:

a = QuantumRegister(1, "a") #ancilla qbit
b = ClassicalRegister(1, "b") #classical bit

c = QuantumCircuit(a, b)

v0 = QuantumRegister(2, "v0")
c.add_register(v0)
c.x(v0[0])

# You can add more registers even if the circuit already contains some gates:
v1 = QuantumRegister(2, "v1")
c.add_register(v1)

# You can use registers the same way regardless of how they added to the circuit:
c.cx(v1[0], a[0])
$\endgroup$
1
3
$\begingroup$

If I am understanding the question correctly, this would be one approach:

from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister

n = 4 # number of vectors to encode

# list of 2 qubit quantum registers to encode n vectors
v = [QuantumRegister(2, 'v'+str(i)) for i in range(n)]

a = QuantumRegister(1, 'a') # ancilla qbit

b = ClassicalRegister(1, 'b') # classical bit

c = QuantumCircuit(*v, a, b)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.