本文整理匯總了Python中qiskit.QuantumCircuit.initialize方法的典型用法代碼示例。如果您正苦於以下問題:Python QuantumCircuit.initialize方法的具體用法?Python QuantumCircuit.initialize怎麽用?Python QuantumCircuit.initialize使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類qiskit.QuantumCircuit
的用法示例。
在下文中一共展示了QuantumCircuit.initialize方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_sympy
# 需要導入模塊: from qiskit import QuantumCircuit [as 別名]
# 或者: from qiskit.QuantumCircuit import initialize [as 別名]
def test_sympy(self):
desired_vector = [
0,
math.cos(math.pi / 3) * complex(0, 1) / math.sqrt(4),
math.sin(math.pi / 3) / math.sqrt(4),
0,
0,
0,
0,
0,
1 / math.sqrt(8) * complex(1, 0),
1 / math.sqrt(8) * complex(0, 1),
0,
0,
0,
0,
1 / math.sqrt(4),
1 / math.sqrt(4) * complex(0, 1)]
qr = QuantumRegister(4, "qr")
qc = QuantumCircuit(qr)
qc.initialize(desired_vector, [qr[0], qr[1], qr[2], qr[3]])
job = wrapper.execute(qc, 'local_statevector_simulator')
result = job.result()
statevector = result.get_statevector()
fidelity = state_fidelity(statevector, desired_vector)
self.assertGreater(
fidelity, self._desired_fidelity,
"Initializer has low fidelity {0:.2g}.".format(fidelity))
示例2: test_single_qubit
# 需要導入模塊: from qiskit import QuantumCircuit [as 別名]
# 或者: from qiskit.QuantumCircuit import initialize [as 別名]
def test_single_qubit(self):
desired_vector = [1/math.sqrt(3), math.sqrt(2)/math.sqrt(3)]
qr = QuantumRegister(1, "qr")
qc = QuantumCircuit(qr)
qc.initialize(desired_vector, [qr[0]])
job = wrapper.execute(qc, 'local_statevector_simulator')
result = job.result()
statevector = result.get_statevector()
fidelity = state_fidelity(statevector, desired_vector)
self.assertGreater(
fidelity, self._desired_fidelity,
"Initializer has low fidelity {0:.2g}.".format(fidelity))
示例3: test_deterministic_state
# 需要導入模塊: from qiskit import QuantumCircuit [as 別名]
# 或者: from qiskit.QuantumCircuit import initialize [as 別名]
def test_deterministic_state(self):
desired_vector = [0, 1, 0, 0]
qr = QuantumRegister(2, "qr")
qc = QuantumCircuit(qr)
qc.initialize(desired_vector, [qr[0], qr[1]])
job = wrapper.execute(qc, 'local_statevector_simulator')
result = job.result()
statevector = result.get_statevector()
fidelity = state_fidelity(statevector, desired_vector)
self.assertGreater(
fidelity, self._desired_fidelity,
"Initializer has low fidelity {0:.2g}.".format(fidelity))
示例4: test_combiner
# 需要導入模塊: from qiskit import QuantumCircuit [as 別名]
# 或者: from qiskit.QuantumCircuit import initialize [as 別名]
def test_combiner(self):
desired_vector = [1, 0]
qr = QuantumRegister(1, "qr")
cr = ClassicalRegister(1, "cr")
qc1 = QuantumCircuit(qr, cr)
qc1.initialize([1.0 / math.sqrt(2), 1.0 / math.sqrt(2)], [qr[0]])
qc2 = QuantumCircuit(qr, cr)
qc2.initialize([1.0 / math.sqrt(2), -1.0 / math.sqrt(2)], [qr[0]])
job = wrapper.execute(qc1+qc2, 'local_statevector_simulator')
result = job.result()
quantum_state = result.get_statevector()
fidelity = state_fidelity(quantum_state, desired_vector)
self.assertGreater(
fidelity, self._desired_fidelity,
"Initializer has low fidelity {0:.2g}.".format(fidelity))
示例5: test_initialize_middle_circuit
# 需要導入模塊: from qiskit import QuantumCircuit [as 別名]
# 或者: from qiskit.QuantumCircuit import initialize [as 別名]
def test_initialize_middle_circuit(self):
desired_vector = [0.5, 0.5, 0.5, 0.5]
qr = QuantumRegister(2, "qr")
cr = ClassicalRegister(2, "cr")
qc = QuantumCircuit(qr, cr)
qc.h(qr[0])
qc.cx(qr[0], qr[1])
qc.reset(qr[0])
qc.reset(qr[1])
qc.initialize(desired_vector, [qr[0], qr[1]])
qc.measure(qr, cr)
# statevector simulator does not support reset
shots = 2000
threshold = 0.04 * shots
job = wrapper.execute(qc, 'local_qasm_simulator', shots=shots)
result = job.result()
counts = result.get_counts()
target = {'00': shots / 4, '01': shots / 4, '10': shots / 4, '11': shots / 4}
self.assertDictAlmostEqual(counts, target, threshold)
示例6: test_random_3qubit
# 需要導入模塊: from qiskit import QuantumCircuit [as 別名]
# 或者: from qiskit.QuantumCircuit import initialize [as 別名]
def test_random_3qubit(self):
desired_vector = [
1 / math.sqrt(16) * complex(0, 1),
1 / math.sqrt(8) * complex(1, 0),
1 / math.sqrt(16) * complex(1, 1),
0,
0,
1 / math.sqrt(8) * complex(1, 2),
1 / math.sqrt(16) * complex(1, 0),
0]
qr = QuantumRegister(3, "qr")
qc = QuantumCircuit(qr)
qc.initialize(desired_vector, [qr[0], qr[1], qr[2]])
job = wrapper.execute(qc, 'local_statevector_simulator')
result = job.result()
statevector = result.get_statevector()
fidelity = state_fidelity(statevector, desired_vector)
self.assertGreater(
fidelity, self._desired_fidelity,
"Initializer has low fidelity {0:.2g}.".format(fidelity))