mitiq
mitiq copied to clipboard
circuit can not convert to mitiq.
i have defined a custom gate in my code. After trying to convert the circuit to Mitiq, i encountered this error:
CircuitConversionError: Circuit could not be converted to an internal Mitiq circuit. This may be because the circuit contains custom gates.
i could fix this error. but i have another error. CircuitConversionError: Circuit could not be converted from an internal Mitiq type to a circuit of type qiskit.
Hey Sasan, thanks for opening an issue! Would you mind providing a minimal working example so that we can see and test the error on our side as well?
def statepreparation(X, circuit):
circuit.ry(X[0], 0)
circuit.cnot(0, 1)
circuit.ry(X[1], 1)
circuit.ry(X[2], 2)
circuit.cnot(1, 2)
circuit.ry(-X[1], 1)
circuit.cnot(0, 2)
return circuit
def qiskit_circuit(X, shots=8192):
circuit1 = QuantumCircuit(3)
circuit1 = statepreparation(X, circuit1)
custom_gate1 = circuit1.to_gate()
custom_gate11 = custom_gate1.control(1)
circuit2 = QuantumCircuit(4, 1)
circuit2.h(0)
circuit2.append(custom_gate11, [0, 1, 2, 3])
return circuit2
circuit = convert_from_mitiq(convert_to_mitiq(qiskit_circuit), "qiskit")
the inner function works without any errors (convert_to_mitiq), but convert_from_mitiq doesn't work.
Hi @sassan72, custom gates and several other front-end specific gates are currently not supported in Mitiq.
From your code snippet it seems that your custom gate is a subcircuit composed of standard elementary gates.
In this case you could still try to use Mitiq as long as you are able to unpack all your custom gates of your circuit such that you can obtain a flat circuit of standard elementary gates. E.g. using something like
qiskit_circuit_before_using_mitiq = qiskit_circuit.decompose()
Many thanks. I will try it.
Hi Sasan, can you confirm whether Andrea's suggestion works for you?
No. It doesn't work with measurement.
I will work on it again and share the error with you.
Were you able to find a workaround?
def statepreparation(X, circuit):
circuit.ry(X[0], 0)
circuit.ry(X[1], 1)
circuit.ry(X[2], 2)
circuit.cnot(0, 1)
circuit.ry(-X[1], 1)
circuit.cnot(0, 2)
circuit.ry(X[3], 2)
circuit.cnot(1, 2)
circuit.ry(X[4], 0)
circuit.ry(X[5], 1)
circuit.ry(X[6], 2)
return circuit
simulator_backend1 = qiskit.Aer.get_backend("qasm_simulator")
def Hadm_test(X1, X2, shots=8192):
c = ClassicalRegister(1)
circuit1 = QuantumCircuit(3)
circuit1 = statepreparation(X1, circuit1)
custom_gate1 = circuit1.to_gate()
custom_gate11 = custom_gate1.control(1)
circuit2 = QuantumCircuit(3)
circuit2 = statepreparation(X2, circuit2)
custom_gate2 = circuit2.to_gate()
custom_gate22 = custom_gate2.control(1)
circuit3 = QuantumCircuit(4, 1)
circuit3.h(0)
circuit3.append(custom_gate11, [0, 1, 2, 3])
circuit3.x(0)
circuit3.append(custom_gate22, [0, 1, 2, 3])
circuit3.x(0)
circuit3.h(0)
circuit3.measure(0, 0)
result = execute(circuit3,simulator_backend1,shots=shots).result()
counts = result.get_counts(circuit3)
result=np.zeros(2)
for key in counts:
result[int(key,2)]=counts[key]
result/=shots
return result
i decompose the controlled rotation and Toffoli gates, but i still get this error: Circuit could not be converted to an internal Mitiq circuit.
Hi @sassan72,
I tried to decompose circuit3
with qiskit.transpile
and it seems to work. Please let us know if you still have problems. This is the code that I used in my test:
import qiskit
from qiskit import ClassicalRegister, QuantumRegister, QuantumCircuit, execute
import numpy as np
from mitiq.interface import convert_to_mitiq, convert_from_mitiq
def statepreparation(X, circuit):
circuit.ry(X[0], 0)
circuit.ry(X[1], 1)
circuit.ry(X[2], 2)
circuit.cnot(0, 1)
circuit.ry(-X[1], 1)
circuit.cnot(0, 2)
circuit.ry(X[3], 2)
circuit.cnot(1, 2)
circuit.ry(X[4], 0)
circuit.ry(X[5], 1)
circuit.ry(X[6], 2)
return circuit
test_state = [1, 2, 3, 4, 5, 6, 7, 8]
test_state = test_state / np.linalg.norm(test_state, 2)
X1 = test_state
X2 = test_state
c = ClassicalRegister(1)
circuit1 = QuantumCircuit(3)
circuit1 = statepreparation(X1, circuit1)
custom_gate1 = circuit1.to_gate()
custom_gate11 = custom_gate1.control(1)
circuit2 = QuantumCircuit(3)
circuit2 = statepreparation(X2, circuit2)
custom_gate2 = circuit2.to_gate()
custom_gate22 = custom_gate2.control(1)
circuit3 = QuantumCircuit(4, 1)
circuit3.h(0)
circuit3.append(custom_gate11, [0, 1, 2, 3])
circuit3.x(0)
circuit3.append(custom_gate22, [0, 1, 2, 3])
circuit3.x(0)
circuit3.h(0)
circuit3.measure(0, 0)
decomposed_circuit = qiskit.transpile(circuit3, basis_gates = ['x', 'h', 'rx','ry' , "cx"])
# Test conversions
cirq_circuit, native_type = convert_to_mitiq(decomposed_circuit)
qiskit_ricuit = convert_from_mitiq(cirq_circuit, native_type)
Thanks a lot. It worked.
Good! :+1: