Skip to content
This repository has been archived by the owner on Jun 12, 2023. It is now read-only.

Commit

Permalink
Remove deprecated Bit.index and Bit.register usage. (#569)
Browse files Browse the repository at this point in the history
Address deprecation warnings raised by Qiskit/qiskit#6069 .
  • Loading branch information
kdk authored Apr 1, 2021
1 parent d6f1ad7 commit 08b134d
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 19 deletions.
6 changes: 5 additions & 1 deletion qiskit/ignis/verification/accreditation/qotp.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ def QOTP_fromlayers(layers, rng):
qotp_circ = QuantumCircuit(qregs, cregs)
temp_circ = QuantumCircuit(qregs, cregs)

bit_indices = {bit: index
for index, bit in enumerate(qotp_circ.qubits)}

# initial z gates after prep
paulizs = rng.randint(2, size=len(qregs))
for qind, q in enumerate(qregs):
Expand Down Expand Up @@ -180,7 +183,8 @@ def QOTP_fromlayers(layers, rng):
qotp_circ.barrier()
twoqindices = []
for _, qsub, _ in gates2q:
twoqindices.append([qsub[0].index, qsub[1].index])
twoqindices.append([bit_indices[qsub[0]],
bit_indices[qsub[1]]])
# update Paulis
for inds in twoqindices:
if twoqubitgate == 'cx':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -664,9 +664,11 @@ def replace_q_indices(circuit, q_nums, qr):
"""

new_circuit = QuantumCircuit(qr)
bit_indices = {bit: index
for index, bit in enumerate(circuit.qubits)}
for instr, qargs, cargs in circuit.data:
new_qargs = [
qr[q_nums[x]] for x in [arg.index for arg in qargs]]
qr[q_nums[x]] for x in [bit_indices[arg] for arg in qargs]]
new_op = copy.deepcopy((instr, new_qargs, cargs))
new_circuit.data.append(new_op)

Expand Down
10 changes: 8 additions & 2 deletions qiskit/ignis/verification/randomized_benchmarking/dihedral.py
Original file line number Diff line number Diff line change
Expand Up @@ -743,8 +743,10 @@ def conjugate(self):
circ = self.to_instruction()
new_circ = QuantumCircuit(self.num_qubits)
qargs = list(range(self.num_qubits))
bit_indices = {bit: index
for index, bit in enumerate(circ.definition.qubits)}
for instr, qregs, _ in circ.definition:
new_qubits = [qargs[tup.index] for tup in qregs]
new_qubits = [qargs[bit_indices[tup]] for tup in qregs]
if instr.name == 'u1':
params = 2 * np.pi - instr.params[0]
instr.params[0] = params
Expand Down Expand Up @@ -878,9 +880,13 @@ def append_circuit(elem, circuit, qargs=None):
raise QiskitError('{} instruction definition is {}; expected QuantumCircuit'.format(
gate.name, type(gate.definition)))

flat_instr = gate.definition
bit_indices = {bit: index
for index, bit in enumerate(flat_instr.qubits)}

for instr, qregs, _ in gate.definition:
# Get the integer position of the flat register
new_qubits = [qargs[tup.index] for tup in qregs]
new_qubits = [qargs[bit_indices[tup]] for tup in qregs]

if (instr.name == 'x' or gate.name == 'x'):
if len(new_qubits) != 1:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,13 @@ def gates_per_clifford(
else:
for transpiled_circuit in transpiled_circuits:
if isinstance(transpiled_circuit, QuantumCircuit):
bit_indices = {bit: index
for index, bit in enumerate(transpiled_circuit.qubits)}

for instr, qregs, _ in transpiled_circuit.data:
for qreg in qregs:
try:
ngates[qreg.index][instr.name] += 1
ngates[bit_indices[qreg]][instr.name] += 1
except KeyError:
pass
else:
Expand Down
18 changes: 9 additions & 9 deletions qiskit/ignis/verification/tomography/basis/circuits.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,22 +388,22 @@ def _tomography_circuits(
raise QiskitError(
"prepared_qubits and measured_qubits are different length.")
num_qubits = len(meas_qubits)
meas_qubit_registers = set(q.register for q in meas_qubits)
# Check qubits being measured are defined in circuit
for reg in meas_qubit_registers:
if reg not in circuit.qregs:
for meas_qubit in meas_qubits:
if meas_qubit not in circuit.qubits:
logger.warning('WARNING: circuit does not contain '
'measured QuantumRegister: %s', reg.name)
'measured qubit: %s', meas_qubit)

prep_qubit_registers = set(q.register for q in prep_qubits)
# Check qubits being measured are defined in circuit
for reg in prep_qubit_registers:
if reg not in circuit.qregs:
for prep_qubit in prep_qubits:
if prep_qubit not in circuit.qubits:
logger.warning('WARNING: circuit does not contain '
'prepared QuantumRegister: %s', reg.name)
'prepared qubit: %s', prep_qubit)

# Get combined registers
qubit_registers = prep_qubit_registers.union(meas_qubit_registers)
qubit_registers = {reg for reg in circuit.qregs
if any(qubit in reg
for qubit in prep_qubits + meas_qubits)}

# Check if there are already measurements in the circuit
for op in circuit:
Expand Down
4 changes: 2 additions & 2 deletions qiskit/ignis/verification/tomography/basis/gatesetbasis.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def measurement_circuit(self,
Returns:
The measurement circuit
"""
circ = QuantumCircuit(qubit.register, clbit.register)
circ = QuantumCircuit([qubit, clbit])
self.add_spam_to_circuit(circ, qubit, op)
circ.measure(qubit, clbit)
return circ
Expand Down Expand Up @@ -191,7 +191,7 @@ def preparation_circuit(self,
Returns:
The preperation circuit
"""
circ = QuantumCircuit(qubit.register)
circ = QuantumCircuit([qubit])
self.add_spam_to_circuit(circ, qubit, op)
return circ

Expand Down
4 changes: 2 additions & 2 deletions qiskit/ignis/verification/tomography/basis/paulibasis.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def pauli_measurement_circuit(
The measurement circuit for the given Pauli.
"""

circ = QuantumCircuit(qubit.register, clbit.register)
circ = QuantumCircuit([qubit, clbit])
if op == 'X':
circ.h(qubit)
circ.measure(qubit, clbit)
Expand Down Expand Up @@ -73,7 +73,7 @@ def pauli_preparation_circuit(
The preparation circuit for the given Pauli eigenstate.
"""

circ = QuantumCircuit(qubit.register)
circ = QuantumCircuit([qubit])
if op == 'Xp':
circ.h(qubit)
if op == 'Xm':
Expand Down
2 changes: 1 addition & 1 deletion qiskit/ignis/verification/tomography/basis/sicbasis.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def sicpovm_preparation_circuit(op: str, qubit: QuantumRegister
Returns:
The preparation circuit
"""
circ = QuantumCircuit(qubit.register)
circ = QuantumCircuit([qubit])
theta = -2 * np.arctan(np.sqrt(2))
if op == 'S1':
circ.append(U3Gate(theta, np.pi, 0.0), [qubit])
Expand Down

0 comments on commit 08b134d

Please sign in to comment.