2
$\begingroup$

to convert my circuit mct instructions to cx I transpiled a Qiskit circuit, but I want to do it without a circuit, because I want more qubits than Qiskit can handle (50 or more).

This is because I'm training a BPN neural net to be a cx gate acting on bloch coordinates. It inputs are control and target qubits bloch coordinates before cx, and outputs the same but after cx.

So how can I change mct instructions to cx, transpiling or what?

$\endgroup$
4
  • $\begingroup$ Hi, Can I ask what you mean when you say that your circuit has more qubits than qiskit can handle? Is your transpilation just taking a long time to run? Or is there some sort of hardcoded limit on circuit size that you are encountering? Thanks :) $\endgroup$
    – Callum
    May 10, 2023 at 16:23
  • $\begingroup$ Yes a 20 qubit transpilation to RX RY RZ X CX H gates takes a lot, so 50 is eternity. So for training the BPN there is no problem, because I can use circuit's with few qubits. but If want to use a set of BPN's for big database search, I need a circuit with lot's of qubit's that Qiskit can not give me. That's why I need a method to convert mct to cx. $\endgroup$ May 10, 2023 at 17:29
  • $\begingroup$ I see. If you don't mind installing the pytket-qiskit package then you could convert your qiskit QuantumCircuit to a pytket Circuit and try to compile/transpile it with TKET istead. No gaurentee that this would work better but could be worth a try. See -> cqcl.github.io/pytket/manual/… . If this interests you I can provide details on how to do this. $\endgroup$
    – Callum
    May 10, 2023 at 21:10
  • $\begingroup$ Also worth noting that for many compiler/transpiler passes the gate count or depth of the circuit can be a bigger contribution to the runtime than the number of qubits. $\endgroup$
    – Callum
    May 10, 2023 at 21:30

2 Answers 2

2
$\begingroup$

I dont think your problem is related with QuantumCircuit, but with synthesis of such as big MCT. By default, the amount of resulting gates grows very quickly (it looks exponentially), as more qubits are involved

from qiskit import QuantumCircuit
from qiskit import transpile

cxs = []
for controlled in (range(3,20)):
    target = 0
    control = list(range(target+1,controlled))
    qc = QuantumCircuit(num)
    qc = QuantumCircuit(num+len(control))
    qc.mcx(control, target)
    result = transpile(qc, basis_gates=['rx','ry','rz','cx'])
    cxs.append(result.count_ops()['cx'])

import matplotlib.pyplot as plt
plt.plot(cxs)
plt.show()

plot qubits vs cxs in the result. Hockey stick graph

With 20 qubits, the default synthesis results in 800K CXs gates.

However, this default synthesis method uses no ancillas. In the mct documentation, should would notice that there are several "modes". The v-chain mode is the most efficient in terms of the result, but uses the biggest amount of ancillas. The amount of resulting cnots grows linearly with the amount of controlled qubits:

from qiskit import QuantumCircuit
from qiskit import transpile

cxs = []
for controlled in (range(3,20)):
    target = 0
    control = list(range(target+1,controlled))
    ancilla = list(range(len(control)+1, 2*len(control)-1))
    qc = QuantumCircuit(num)
    qc = QuantumCircuit(num+len(control)+len(ancilla))
    qc.mcx(control, target, ancilla, mode='v-chain')  # <-
    result = transpile(qc, basis_gates=['rx','ry','rz','cx'])
    cxs.append(result.count_ops()['cx'])

import matplotlib.pyplot as plt
plt.plot(cxs)
plt.show()

linear groth

So, you can create a quantum circuit that considers these ancillas, it would look something like this:

from qiskit import QuantumCircuit
from qiskit import transpile

target = 0 # <- qubit 0 as a target 
control = list(range(target+1,60)) # <- controlled qubits
ancilla = list(range(len(control)+1, 2*len(control)-1)) # <- ancilla qubits
print('target', target)
print('control', control)
print('ancilla', ancilla)

qc = QuantumCircuit(num)
qc = QuantumCircuit(num+len(control)+len(ancilla))
qc.mcx(control, target, ancilla, mode='v-chain')
result = transpile(qc, basis_gates=['rx','ry','rz','cx'])

print(result.count_ops())
target 0
control [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59]
ancilla [60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116]
OrderedDict([('rz', 463), ('cx', 348), ('ry', 230), ('rx', 1)])
$\endgroup$
9
  • $\begingroup$ As I said with cirq.decompose_multi_controlled_x with free qubits = num controls -2 gives many many much less gates than qiskit. I tested both. $\endgroup$ Jul 6, 2023 at 18:25
  • $\begingroup$ same for mct than for mcx? $\endgroup$
    – luciano
    Jul 6, 2023 at 20:52
  • $\begingroup$ I don't know exactly the difference between both, but i did it for mct. $\endgroup$ Jul 6, 2023 at 22:57
  • $\begingroup$ For example in Qiskit 9 controls translated to cx and rx ry rz are 3067 gates, while in cirq are 212 gates. More qubits, bigger difference between both $\endgroup$ Jul 6, 2023 at 23:07
  • $\begingroup$ Here is a .py that generates a function with the gates for a number of controls as parameter with the cirq method. github.com/jesusmontera/varios/blob/… $\endgroup$ Jul 6, 2023 at 23:23
0
$\begingroup$

Fist use cirq.decompose_multi_controlled_x with num free qubits = num controls -2 and then transpile each ccx( always 2) with qiskit (15 instructions each). That works fast for lot's of qubits and give much less instrucions than qiskit transpile

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.