面试题答案
一键面试class Qubit
def initialize(state)
@state = state == '|0⟩' ? '|0⟩' : '|1⟩'
end
def get_state
@state
end
end
在上述代码中:
- 定义了
Qubit
类。 initialize
方法用于初始化量子比特的状态,确保传入的状态为|0⟩
或|1⟩
,如果不是|0⟩
,则设为|1⟩
。get_state
方法用于获取当前量子比特的状态。