1

I work on theory tasks in quantum computing, and make simple experiments with Qiskit. Unfortunately, I can't find a way how to make a complex control gates there, where control is in the quantum register.

I would like to have a "c_if" analogue, which can be chained and use quantum bits as a control. Smth like

swap(q1, q2).c_if(q0,Zero).c_if(q3,One)

Is there such an operation in the qiskit? How could I emulate such an operation if it doesn't exist?

enter image description here

Oxoron
  • 664
  • 1
  • 7
  • 26
  • Correct me if I'm wrong, but if you want qubits as controls, and other qubits as targets, you should be able to use any of the controlled gates within Qiskit. The most common of of these being the CNOT gate, which takes a qubit as a target, and flips it if the control qubit is in the 1 state. Is this functionality what you are looking for? – Matthew Stypulkoski Apr 20 '20 at 14:04
  • @MatthewStypulkoski you're right. I need complex CNOTs and CWAPs. I found a .control() function, but it's doesn't work at the moment. – Oxoron Apr 20 '20 at 20:33
  • 1
    Would something similar to the following work? CNOT on q3, q0 (ctrl, trgt) followed by a CSWAP on q0, q1, q2 (c, t, t). The CNOT covers (if q3 == 1), and the following CSWAP would cover (if q0 == 0) because if the swap goes off, which is what you want, then that means q0 == 1, which would have meant q0 == 0 and was flipped by the previous CNOT. I think this should work, unless I am misunderstanding. – Matthew Stypulkoski Apr 21 '20 at 15:06
  • @MatthewStypulkoski this may work, but that's a workaround. I'm experimenting a lot, and don't want to spend time on the micro control of the operations. – Oxoron Apr 21 '20 at 16:29

2 Answers2

2

Check out Qiskit documentation for the MCXGate, know as the Multi-controlled-X Gate. This gate lets you define how many control qubits you would like to include (perhaps the majority of your quantum register) and define a control state.


from qiskit import * 

my_circuit = QuantumRegister(3,3)

my_circuit.append(circuit.library.MCXGate(2, ctrl_state='10'), [0,1,2]) 
   

Check out the documentation here.

There are also variations that will do Y gate Z gate or whatever you like depending if the circuit sees the correct control gate.

Dulah
  • 66
  • 5
  • 1
    Thanks @Dulah . There was a bug related to this functionality, but looks like it's been fixed since the time the question was raised. Few more samples: #control1 = XGate().control(3, None, '100') #: old-style multy-controlled qubits control1 = MCXGate(3, None, '110') # fashion-style multi-controlled qubits – Oxoron Sep 24 '21 at 19:05
1

Thanks @Dulah for his answer. I found my old samples and they're working pretty fine with the 0.18.2 qiskit version.

from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit, execute, Aer
from qiskit.circuit.library.standard_gates.x import XGate, MCXGate
from qiskit.circuit.library.standard_gates.swap import SwapGate

simulator = Aer.get_backend('qasm_simulator')
qreg = QuantumRegister(4) 
creg = ClassicalRegister(4)
qc = QuantumCircuit(qreg, creg)


control1 =  XGate().control(3, None, '110') #: old-style multy-controlled qubits
#control1 = MCXGate(3, None, '110') # fashion-style multi-controlled qubits

control2 = SwapGate().control(2, None, '10')

qc.append(control1, [0, 1, 2, 3])
qc.append(control2, [0, 1, 2, 3])
qc.measure(qreg,creg)   

job = execute(qc, simulator, shots=1000)
result = job.result()
counts = result.get_counts(qc)
print("\nTotal count for 00 and 11 are:",counts)
qc.draw()

The code gives me a result

enter image description here

Oxoron
  • 664
  • 1
  • 7
  • 26