Skip to content

Commit 102929f

Browse files
Swap Control and Target (#2311)
In circuit editing, when moving a control onto a target or vise-versa, the control and target should swap positions. ![Swap control target](https://github.com/user-attachments/assets/29a422d4-fc2c-4b5e-9021-2bdd946ad897) This also fixes an issue with copy-moving controls. The behavior is now that it will create a new control for the same operation and possibly move the operation horizontally in the circuit. ![Copy-move control](https://github.com/user-attachments/assets/462253ec-f7a1-453c-8b57-e2b744104755)
1 parent a529cc2 commit 102929f

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

npm/qsharp/ux/circuit-vis/circuitManipulation.ts

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -129,27 +129,49 @@ const _moveY = (
129129
movingControl: boolean,
130130
): void => {
131131
// Check if the source operation already has a target or control on the target wire
132-
let registers: Register[];
132+
let targets: Register[];
133133
switch (sourceOperation.kind) {
134134
case "unitary":
135-
registers = [
136-
...sourceOperation.targets,
137-
...(sourceOperation.controls || []),
138-
];
135+
case "ket":
136+
targets = sourceOperation.targets;
139137
break;
140138
case "measurement":
141-
registers = [...sourceOperation.qubits, ...sourceOperation.results];
139+
targets = sourceOperation.qubits;
140+
break;
141+
}
142+
143+
let controls: Register[];
144+
switch (sourceOperation.kind) {
145+
case "unitary":
146+
controls = sourceOperation.controls || [];
142147
break;
148+
case "measurement":
143149
case "ket":
144-
registers = sourceOperation.targets;
150+
controls = [];
145151
break;
146152
}
147153

148-
if (registers.find((target) => target.qubit === targetWire)) {
149-
// If the target or control already exists, don't move the target/control
154+
let likeRegisters: Register[];
155+
let unlikeRegisters: Register[];
156+
if (movingControl) {
157+
likeRegisters = controls;
158+
unlikeRegisters = targets;
159+
} else {
160+
likeRegisters = targets;
161+
unlikeRegisters = controls;
162+
}
163+
164+
// If a similar register already exists, don't move the gate
165+
if (likeRegisters.find((reg) => reg.qubit === targetWire)) {
150166
return;
151167
}
152168

169+
// If a different kind of register already exists, swap the control and target
170+
if (unlikeRegisters.find((reg) => reg.qubit === targetWire)) {
171+
const index = unlikeRegisters.findIndex((reg) => reg.qubit === targetWire);
172+
unlikeRegisters[index].qubit = sourceWire;
173+
}
174+
153175
switch (sourceOperation.kind) {
154176
case "unitary":
155177
if (movingControl) {

npm/qsharp/ux/circuit-vis/events.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ class CircuitEvents {
483483
targetLoc,
484484
this.selectedWire,
485485
targetWire,
486-
false,
486+
this.movingControl,
487487
insertNewColumn,
488488
);
489489
} else {

0 commit comments

Comments
 (0)