6

I'm trying to initialize a qubit with a custom state in IBM's Qiskit Composer. I wrote the code in the Qiskit Lab and obtained the QASM code as shown below

OPENQASM 2.0;
include "qelib1.inc";

qreg q0[1]; creg c[1];

initialize(0.5,0.8660254) q0[0];

This is giving error stating initialize is not defined. What's the way to initialize a qubit with a custom value in Qiskit Composer?

Van Peer
  • 627
  • 1
  • 7
  • 15

2 Answers2

5

What you can do is use qiskit to decompose the circuit with the initialize instruction to elementary gate, then print out the QASM code that you can paste into the Composer.

For example: Using the vector you want to initialize, I can do

from qiskit import QuantumCircuit, IBMQ
import numpy as np
num_qubits = 1
vector = [0.5,0.8660254]
initial_state = vector/np.linalg.norm(vector)
circuit = QuantumCircuit(num_qubits,num_qubits)
circuit.initialize(initial_state, 0)  
print(circuit)
qasm_circuit = circuit.decompose().decompose().decompose() 
print(qasm_circuit)
 ┌─────────────────────────┐

q_0: ┤ initialize(0.5,0.86603) ├ └─────────────────────────┘ c: 1/═══════════════════════════

      ┌──────────┐

q_0: ─|0>─┤ RY(2π/3) ├ └──────────┘ c: 1/═════════════════

Then now, if I do:

print(qasm_circuit.qasm())

then I will get

OPENQASM 2.0;
include "qelib1.inc";
qreg q[1];
creg c[1];
reset q[0];
ry(2*pi/3) q[0];

You can now paste the QASM code to the Composer and continue on with your circuit construction.

KAJ226
  • 14,252
  • 2
  • 13
  • 34
5

TL;DR The initialize instruction is not supported by IBM Quantum Circuit Composer. However, you can use the transpiler to reconvert it in terms of the supported basis:

circuit = transpile(circuit, basis_gates=["u3","u2","u1","cx","id","u0","u","p","x","y","z","h","s","sdg","t","tdg","rx","ry","rz","sx","sxdg","cz","cy","swap","ch","ccx","cswap","crx","cry","crz","cu1","cp","cu3","csx","cu","rxx","rzz","rccx","rc3x","c3x","c3sqrtx","c4x"])

The long version:

The IBM Quantum Circuit Composer does not allow gates outside of the "standard library". The IBM Quantum Lab raises an error explaining the situation:

from qiskit import QuantumCircuit
from ibm_quantum_widgets import CircuitComposer

circuit = QuantumCircuit(1) circuit.initialize([0, 1], 0)

editor = CircuitComposer(circuit=circuit) editor

enter image description here

You can use transpile to rewrite the circuit in terms of the supported gates in the standard library. Here is a generic way to do that:

from qiskit import QuantumCircuit, transpile
from ibm_quantum_widgets import CircuitComposer

circuit = QuantumCircuit(1) circuit.initialize([0, 1], 0) circuit = transpile(circuit, basis_gates=["u3","u2","u1","cx","id","u0","u","p","x","y","z","h","s","sdg","t","tdg","rx","ry","rz","sx","sxdg","cz","cy","swap","ch","ccx","cswap","crx","cry","crz","cu1","cp","cu3","csx","cu","rxx","rzz","rccx","rc3x","c3x","c3sqrtx","c4x"]) editor = CircuitComposer(circuit=circuit) editor

enter image description here

This transpiled circuit is dumplable as QASM too:

print(circuit.qasm())
OPENQASM 2.0;
include "qelib1.inc";
qreg q[1];
ry(pi) q[0];
luciano
  • 6,164
  • 1
  • 14
  • 34