-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Closed
Description
When inserting gates with Circuit.insert(), the gates are (often) added to the previous moment. I assume the fault is in _pick_or_create_inserted_op_moment_index
To replicate the error, the following code shows the problem. I start with a circuit of three CNOTs (one per moment), remove one of the CNOTS (expect three moments with one being empty), insert the same gate back to the Moment index_to_clean, but the gate is added to the previous Moment.
For the following code I get a strange result
import cirq
q = [cirq.NamedQubit("q" + str(i)) for i in range(4)]
circ = cirq.Circuit()
index_to_clean = 1
cnots = [
cirq.ops.CNOT.on(q[0], q[3]),
cirq.ops.CNOT.on(q[1], q[2]),
cirq.ops.CNOT.on(q[1], q[2])
]
circ.append(cnots, strategy=cirq.InsertStrategy.NEW)
print("Fresh circuit", repr(circ))
circ.clear_operations_touching(cnots[index_to_clean].qubits, [index_to_clean])
print("Removed CNOT at {}".format(index_to_clean), repr(circ))
circ.insert(index_to_clean, cnots[index_to_clean], strategy=cirq.InsertStrategy.INLINE)
print("Inserted CNOT at {}".format(index_to_clean), repr(circ))
The output is
Fresh circuit cirq.Circuit([
cirq.Moment(operations=[
cirq.CNOT.on(cirq.NamedQubit('q0'), cirq.NamedQubit('q3')),
]),
cirq.Moment(operations=[
cirq.CNOT.on(cirq.NamedQubit('q1'), cirq.NamedQubit('q2')),
]),
cirq.Moment(operations=[
cirq.CNOT.on(cirq.NamedQubit('q1'), cirq.NamedQubit('q2')),
]),
])
Removed CNOT at 1 cirq.Circuit([
cirq.Moment(operations=[
cirq.CNOT.on(cirq.NamedQubit('q0'), cirq.NamedQubit('q3')),
]),
cirq.Moment(),
cirq.Moment(operations=[
cirq.CNOT.on(cirq.NamedQubit('q1'), cirq.NamedQubit('q2')),
]),
])
Inserted CNOT at 1 cirq.Circuit([
cirq.Moment(operations=[
cirq.CNOT.on(cirq.NamedQubit('q0'), cirq.NamedQubit('q3')),
cirq.CNOT.on(cirq.NamedQubit('q1'), cirq.NamedQubit('q2')),
]),
cirq.Moment(),
cirq.Moment(operations=[
cirq.CNOT.on(cirq.NamedQubit('q1'), cirq.NamedQubit('q2')),
]),
])
Metadata
Metadata
Assignees
Labels
No labels