2

I'll start with the problem I have relative to the multiplexer, I built a simple 2*1 Mux using just CX, but from what I understood Qiskit has the gate UCGate that can be used as multiplexer so I am trying to implement a multiplexer with n controls, 2^n inputs and one output so that once an input is chosen with control qubits its state 0 or 1 is passed to the output, the code I have is this

def multiplexer(n_controls):
    num_inputs = 2 ** n_controls  
    total_qubits = n_controls + num_inputs + 1
qc = QuantumCircuit(total_qubits)

identity = np.eye(2)  
unitary_list = [identity] * num_inputs 
uc_gate = UCGate(unitary_list)  
qc.x(3)

#This is the part that gives most problems i cant figure out how to set it up
qc.append(uc_gate.control(n_controls), list(range(n_controls)) + [total_qubits - 1])  

qc.measure_all()

return qc

qc = multiplexer(3) qc.draw('mpl')

But I can't get to assign it the right controls, inputs and outputs, can you help?

For the Quantum full Adder I have a simple one working and my question is just how I can build a n-bit one, for the 2 bit I have this simple code:

from qiskit import QuantumCircuit

qc = QuantumCircuit(4) qc.ccx(0,1,3) qc.cx(0,1) qc.ccx(1,2,3) qc.cx(1,2) qc.cx(0,1)

qc.measure_all() qc.draw("mpl")

Thanks for the help

1 Answers1

2

The UCGate cannot be used for what you are trying to accomplish because it only allows to control single-qubit gates, but what you need are CX gates between your inputs and your output conditioned on the control qubits.

The best way to accomplish this is by using multi-controlled X gates. The code below applies CX gates with the control on each of the inputs, and the target on the output to "copy" the basis state from input to output. Each of these gates is then controlled by the select qubits accordingly:

import numpy as np
from qiskit import QuantumCircuit, QuantumRegister
from qiskit.circuit.library import CXGate

def multiplexer(n_controls):

n_inputs = 2**n_controls
qr_sel = QuantumRegister(n_controls,name='sel')
qr_inp = QuantumRegister(n_inputs, name='inp')
qr_out = QuantumRegister(1, name='out')

qc = QuantumCircuit(qr_out, qr_inp, qr_sel)

for i in range(n_inputs):
    i_bin = np.binary_repr(i,n_controls)
    mcx = CXGate().control(n_controls,ctrl_state=i_bin)
    qc.append(mcx,qr_sel[:]+[qr_inp[i]]+qr_out[:])

qc.measure_all()

return qc

qc = multiplexer(3)

This generates the following circuit, which should be what you're looking for:

enter image description here

diemilio
  • 3,043
  • 1
  • 6
  • 16