mirror of https://github.com/Qiskit/qiskit.git
Fix depth for barriers and snapshots (#2748)
* fix depth for barriers and snapshots * Delete Untitled.ipynb * fix lint not playing nice with snapshot import
This commit is contained in:
parent
365b790b95
commit
fae93bc413
|
@ -624,30 +624,39 @@ class QuantumCircuit:
|
|||
# line so that they all stacked at the same depth.
|
||||
# Conditional gates act on all cbits in the register
|
||||
# they are conditioned on.
|
||||
# We do not consider barriers or snapshots as
|
||||
# We treat barriers or snapshots different as
|
||||
# They are transpiler and simulator directives.
|
||||
# The max stack height is the circuit depth.
|
||||
for instr, qargs, cargs in self.data:
|
||||
if instr.name not in ['barrier', 'snapshot']:
|
||||
levels = []
|
||||
reg_ints = []
|
||||
for ind, reg in enumerate(qargs + cargs):
|
||||
# Add to the stacks of the qubits and
|
||||
# cbits used in the gate.
|
||||
reg_ints.append(reg_map[reg.register.name] + reg.index)
|
||||
levels = []
|
||||
reg_ints = []
|
||||
# If count then add one to stack heights
|
||||
count = True
|
||||
if instr.name in ['barrier', 'snapshot']:
|
||||
count = False
|
||||
for ind, reg in enumerate(qargs + cargs):
|
||||
# Add to the stacks of the qubits and
|
||||
# cbits used in the gate.
|
||||
reg_ints.append(reg_map[reg.register.name] + reg.index)
|
||||
if count:
|
||||
levels.append(op_stack[reg_ints[ind]] + 1)
|
||||
if instr.control:
|
||||
# Controls operate over all bits in the
|
||||
# classical register they use.
|
||||
cint = reg_map[instr.control[0].name]
|
||||
for off in range(instr.control[0].size):
|
||||
if cint + off not in reg_ints:
|
||||
reg_ints.append(cint + off)
|
||||
levels.append(op_stack[cint + off] + 1)
|
||||
else:
|
||||
levels.append(op_stack[reg_ints[ind]])
|
||||
# Assuming here that there is no controlled
|
||||
# snapshots or barriers ever.
|
||||
if instr.control:
|
||||
# Controls operate over all bits in the
|
||||
# classical register they use.
|
||||
cint = reg_map[instr.control[0].name]
|
||||
for off in range(instr.control[0].size):
|
||||
if cint + off not in reg_ints:
|
||||
reg_ints.append(cint + off)
|
||||
levels.append(op_stack[cint + off] + 1)
|
||||
|
||||
max_level = max(levels)
|
||||
for ind in reg_ints:
|
||||
op_stack[ind] = max_level
|
||||
|
||||
max_level = max(levels)
|
||||
for ind in reg_ints:
|
||||
op_stack[ind] = max_level
|
||||
return max(op_stack)
|
||||
|
||||
def width(self):
|
||||
|
|
|
@ -20,6 +20,8 @@ import unittest
|
|||
import numpy as np
|
||||
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
|
||||
from qiskit.test import QiskitTestCase
|
||||
# pylint: disable=unused-import
|
||||
from qiskit.extensions.simulator import snapshot
|
||||
|
||||
|
||||
class TestCircuitProperties(QiskitTestCase):
|
||||
|
@ -227,6 +229,93 @@ class TestCircuitProperties(QiskitTestCase):
|
|||
qc.measure(q[3], c[0])
|
||||
self.assertEqual(qc.depth(), 5)
|
||||
|
||||
def test_circuit_depth_barriers1(self):
|
||||
"""Test circuit depth for barriers #1.
|
||||
"""
|
||||
q = QuantumRegister(4, 'q')
|
||||
c = ClassicalRegister(4, 'c')
|
||||
circ = QuantumCircuit(q, c)
|
||||
circ.h(0)
|
||||
circ.cx(0, 1)
|
||||
circ.barrier(q)
|
||||
circ.h(2)
|
||||
circ.cx(2, 3)
|
||||
self.assertEqual(circ.depth(), 4)
|
||||
|
||||
def test_circuit_depth_barriers2(self):
|
||||
"""Test circuit depth for barriers #2.
|
||||
"""
|
||||
q = QuantumRegister(4, 'q')
|
||||
c = ClassicalRegister(4, 'c')
|
||||
circ = QuantumCircuit(q, c)
|
||||
circ.h(0)
|
||||
circ.barrier(q)
|
||||
circ.cx(0, 1)
|
||||
circ.barrier(q)
|
||||
circ.h(2)
|
||||
circ.barrier(q)
|
||||
circ.cx(2, 3)
|
||||
self.assertEqual(circ.depth(), 4)
|
||||
|
||||
def test_circuit_depth_barriers3(self):
|
||||
"""Test circuit depth for barriers #3.
|
||||
"""
|
||||
q = QuantumRegister(4, 'q')
|
||||
c = ClassicalRegister(4, 'c')
|
||||
circ = QuantumCircuit(q, c)
|
||||
circ.h(0)
|
||||
circ.barrier(q)
|
||||
circ.cx(0, 1)
|
||||
circ.barrier(q)
|
||||
circ.barrier(q)
|
||||
circ.barrier(q)
|
||||
circ.h(2)
|
||||
circ.barrier(q)
|
||||
circ.cx(2, 3)
|
||||
self.assertEqual(circ.depth(), 4)
|
||||
|
||||
def test_circuit_depth_snap1(self):
|
||||
"""Test circuit depth for snapshots #1.
|
||||
"""
|
||||
q = QuantumRegister(4, 'q')
|
||||
c = ClassicalRegister(4, 'c')
|
||||
circ = QuantumCircuit(q, c)
|
||||
circ.h(0)
|
||||
circ.cx(0, 1)
|
||||
circ.snapshot('snap')
|
||||
circ.h(2)
|
||||
circ.cx(2, 3)
|
||||
self.assertEqual(circ.depth(), 4)
|
||||
|
||||
def test_circuit_depth_snap2(self):
|
||||
"""Test circuit depth for snapshots #2.
|
||||
"""
|
||||
q = QuantumRegister(4, 'q')
|
||||
c = ClassicalRegister(4, 'c')
|
||||
circ = QuantumCircuit(q, c)
|
||||
circ.h(0)
|
||||
circ.snapshot('snap0')
|
||||
circ.cx(0, 1)
|
||||
circ.snapshot('snap1')
|
||||
circ.h(2)
|
||||
circ.snapshot('snap2')
|
||||
circ.cx(2, 3)
|
||||
self.assertEqual(circ.depth(), 4)
|
||||
|
||||
def test_circuit_depth_snap3(self):
|
||||
"""Test circuit depth for snapshots #3.
|
||||
"""
|
||||
q = QuantumRegister(4, 'q')
|
||||
c = ClassicalRegister(4, 'c')
|
||||
circ = QuantumCircuit(q, c)
|
||||
circ.h(0)
|
||||
circ.cx(0, 1)
|
||||
circ.snapshot('snap0')
|
||||
circ.snapshot('snap1')
|
||||
circ.h(2)
|
||||
circ.cx(2, 3)
|
||||
self.assertEqual(circ.depth(), 4)
|
||||
|
||||
def test_circuit_size_empty(self):
|
||||
"""Circuit.size should return 0 for an empty circuit."""
|
||||
size = 4
|
||||
|
|
Loading…
Reference in New Issue