如何从 qiskit 中的 np.array 创建单一门?

Vox*_*Vox 6 python quantum-computing qiskit

大家好:Cirq 提供了一种从数组创建单一门的方法。我尝试在 Qiskit 中做同样的事情,但未能完全成功。这是一个示例代码,其中包含到目前为止我可以组合的内容。另外,有没有办法应用从 q[0] 到 q[1] 的单一受控操作?或者为此目的创建一个特定的标记门以在电路中使用?如果是这样,怎么办?多谢!

from qiskit.extensions import *
U2x2 = np.array([[0.998762, -0.049745], [-0.049745, -0.998762]])
# Still not sure how to use this, though it compiles
gate2x2 = UnitaryGate(U2x2)

# The best I could do so far was this:
# Create the quantum circuit
q = QuantumRegister(2)
c = ClassicalRegister(2)
qc = QuantumCircuit(q, c)

qc.unitary(U2x2, range(1))
qc.measure(q[0], c[0])
Run Code Online (Sandbox Code Playgroud)

小智 4

qc.unitary(U2x2, range(1))如果您只想从数组中获得常规的单一门,我相信您的实现是正确的。实例化 aUnitaryGate似乎已经在qc.unitary()调用中完成,因此只需调用qc.unitary()就可以了。

但是,如果您想要此单一门的受控版本,我发现UnitaryGate手动实例化然后向该门添加控件效果很好。与此类似的东西应该有效:

from qiskit.circuit.add_control import add_control

gate2x2 = UnitaryGate(U2x2)
gate2x2_ctrl = add_control(gate2x2, 1)

qc.append(gate2x2_ctrl, [q[0], q[1]])
Run Code Online (Sandbox Code Playgroud)

add_control()如果您需要更多信息,这里还有源代码。