mitiq icon indicating copy to clipboard operation
mitiq copied to clipboard

circuit can not convert to mitiq.

Open sassan72 opened this issue 2 years ago • 11 comments

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.

sassan72 avatar Jun 13 '22 06:06 sassan72

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.

sassan72 avatar Jun 13 '22 07:06 sassan72

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?

natestemen avatar Jun 13 '22 16:06 natestemen

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.

sassan72 avatar Jun 14 '22 07:06 sassan72

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()

andreamari avatar Jun 14 '22 10:06 andreamari

Many thanks. I will try it.

sassan72 avatar Jun 14 '22 10:06 sassan72

Hi Sasan, can you confirm whether Andrea's suggestion works for you?

natestemen avatar Jul 08 '22 04:07 natestemen

No. It doesn't work with measurement.

sassan72 avatar Jul 14 '22 20:07 sassan72

I will work on it again and share the error with you.

sassan72 avatar Jul 14 '22 20:07 sassan72

Were you able to find a workaround?

natestemen avatar Aug 08 '22 17:08 natestemen

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

sassan72 avatar Aug 09 '22 10:08 sassan72

i decompose the controlled rotation and Toffoli gates, but i still get this error: Circuit could not be converted to an internal Mitiq circuit.

sassan72 avatar Aug 10 '22 09:08 sassan72

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)

andreamari avatar Sep 06 '22 08:09 andreamari

Thanks a lot. It worked.

sassan72 avatar Sep 06 '22 09:09 sassan72

Good! :+1:

andreamari avatar Sep 06 '22 13:09 andreamari