This paper simulates duality mode to solve sudoku puzzles. The last diagram in the paper (shown below) illustrates multi-qubit controlled gates with two Z/X gates. I wonder if Qiskit can directly implement these gates.
1 Answer
As @FrankYellin mentioned in the comments, this is theoretically equivalent to two MCXGate
applied successively (up to some HGate
for the $Z$ version).
However, you shouldn't worry about this, a framework's goal is to let it deal with low-level implementation, while you should care about what operation you want to perform. Fundamentally, what you're looking for is a multi-controlled $XX$ gate.
Qiskit has an RXXGate
, but this will include an unwanted relative phase. You can create the $XX$ gate like this:
from qiskit import QuantumCircuit
circ = QuantumCircuit(2)
circ.x([0, 1])
xx_gate = circ.to_gate(label="XXGate")
Now that we have our $XX$ gate, we can control it out of other qubits as desired, like so:
from qiskit import QuantumRegister
# Be careful about Qiskit's little-endian convention
controlled_xx_gate = xx_gate.control(7, "MCXXGate", "0000100")
alpha = QuantumRegister(4, "alpha")
beta = QuantumRegister(4, "beta")
gamma = QuantumRegister(4, "gamma")
circ = QuantumCircuit(alpha, beta, gamma)
circ.h([alpha[-1], beta[-1], gamma[-1]])
# Using HXH = Z
circ.h([beta[0], gamma[0]])
# Be careful about Qiskit's little-endian convention
circ.append(controlled_xx_gate, [*alpha[:-1], beta[1], beta[-1], gamma[1], gamma[-1], beta[0], gamma[0]])
circ.h([beta[0], gamma[0]])
# Be careful about Qiskit's little-endian convention
circ.append(controlled_xx_gate, [*alpha[:-1], beta[1], beta[-1], gamma[1], gamma[-1], beta[0], gamma[0]])
The resulting circuit looks like this:
-
$\begingroup$ What does "*" do in the last line of code? Which code creates anti-controlled gate? $\endgroup$– YiliCommented Nov 29, 2022 at 15:51
-
1$\begingroup$ @Yili
*alpha[:-1]
is a shorthand foralpha[0], alpha[1], alpha[2]
. More generally,*my_list
"unwraps" the list. The controls of the gate are defined via the string"0000100"
, which means that the gate will be applied if the first 4 qubits are 0, the fifth one 1 and the last ones 0. Note that because of Qiskit's convention to use little-endian, this has to be read from bottom to top wire-wise. $\endgroup$– Tristan Nemoz ♦Commented Nov 29, 2022 at 18:15 -
$\begingroup$ Which part of codes do you specify which wire(s) to append xx_gate within the gate "MCXXGate"? $\endgroup$– YiliCommented Nov 30, 2022 at 16:12
-
$\begingroup$ @Yili This one:
controlled_xx_gate = xx_gate.control(7, "MCXXGate", "0000100")
. This code transforms thexx_gate
to a controlled version of it, which I labelledMCXXGate
, controlled by the string"0000100"
. The convention in Qiskit when creating such a gate is that the first inputs are the controls, and the last ones the inputs to the original gate, which is why, when applying it, we passbeta[0]
andgamma[0]
as the last parameters: these are the registers on which we wish to apply the $XX$ gate $\endgroup$– Tristan Nemoz ♦Commented Nov 30, 2022 at 16:17