pyqpanda_alg.QCmp¶
Submodules¶
Functions¶
|
This function provides comparators to compare basis states(can be superposition states) |
|
This function provides comparators to compare basis states(generate from a smooth distribution) |
|
This function provides comparators to compare between two basis states(can be superposition states). |
|
This function provides qft_based comparators to compare basis states(can be superposition states) |
|
This function provides qft_based comparators to compare between two basis states(can be superposition states). |
Package Contents¶
- pyqpanda_alg.QCmp.int_comparator(value, q_state, q_anc_cmp, function='geq', reuse=False)¶
This function provides comparators to compare basis states(can be superposition states) against a given classical integer.
- Parameters
value :
intThe given classical integer.
q_state :
Qubit,QVecState qubits.
q_anc_cmp :
QVecAncilla and comparison result qubits. The comparison result qubit should be the last element. The qubit number of this register should be equal to q_state.
function :
str{'geq', 'g', 'seq', 's'}, optionalEvaluate conditions:
geq: evaluate a>=condition. (Default)g: evaluate a>condition.seq: evaluate a<=condition.s: evaluate a<condition.
reuse :
boolSet to True to add a reverse circuit part to reuse ancilla qubits.
- Returns
circuit :
QCircuitThe result this function return is a quangtum circuit. The comparison result qubit would be in state \(|1\rangle\) when the quantum state satisfies the comparison condition, otherwise \(|0\rangle\). Therefore we can get a probability outcome of the comparison.
- Examples
An example of implementing a two-qubit uniform superposition state compared with 2.
>>> from pyqpanda_alg import QCmp >>> import numpy as np >>> from pyqpanda3.core import * >>> value = 2 >>> m = CPUQVM() >>> prog = QProg() >>> prog << H(0) << H(1) >>> cir = QCmp.int_comparator(value, [0, 1], [2, 3], function='g', reuse=True) >>> prog << cir >>> m.run(prog, 1000) >>> prob_dict_result = m.result().get_prob_dict([3]) >>> print(prob_dict_result) {'0': 0.7500000000000003, '1': 0.2500000000000001}
- pyqpanda_alg.QCmp.interpolation_comparator(value, q_state, q_anc_cmp, function='g', reuse=False)¶
This function provides comparators to compare basis states(generate from a smooth distribution) against a given classical number(can be float). This function introduces an interpolation method to make qubits “look” like a float number when compared, so that comparisons can be made in the real number domain. In detail, when sampling, we regard the quantum state
aas the interval frominf:=a-0.5tosup:=a+1.5. When comparing the quantum state withinf+delta, the probability of smaller is delta.- Parameters
value :
floatThe given classical number.
q_state :
Qubit,QVecState qubits.
q_anc_cmp :
QVecAncilla and comparison result qubits. The comparison result qubit should be the last element. The qubit number of this register should be equal to q_state.
function :
str{'g', 's'}, optionalEvaluate conditions:
g: evaluate a>condition.(Default)s: evaluate a<condition.
reuse :
boolSet to True to add a reverse circuit part to reuse ancilla qubits.
- Returns
circuit :
QCircuitThe result this function return is a quangtum circuit. The comparison result qubit would be in state \(|1\rangle\) when the quantum state satisfies the comparison condition, otherwise \(|0\rangle\). Therefore we can get a probability outcome of the comparison.
- Examples
An example of implementing qubit state ‘110’ compared with 3.3.
>>> from pyqpanda_alg import QCmp >>> import numpy as np >>> from pyqpanda3.core import * >>> value = 3.3 >>> m = CPUQVM() >>> prog = QProg() >>> prog << X(0) << X(1) << I(2) >>> cir = QCmp.interpolation_comparator(value, [0, 1, 2], [3, 4, 5], function='g', reuse=True) >>> prog << cir >>> m.run(prog, 1000) >>> prob_dict_result = m.result().get_prob_dict([5]) >>> print(prob_dict_result) {'0': 0.7999999999999997, '1': 0.20000000000000023}
- pyqpanda_alg.QCmp.qubit_comparator(q_state_1, q_state_2, q_anc_cmp, function='geq')¶
This function provides comparators to compare between two basis states(can be superposition states).
- Parameters
q_state_1 :
Qubit,QVecThe first state qubits.
q_state_2 :
Qubit,QVecThe second state qubits.
q_anc_cmp :
QVecAncilla and comparison result qubits. The comparison result qubit should be the last element. The qubit number of this register should be equal to q_state.
function :
str{'geq', 'g', 'seq', 's', 'eq', 'neq'}, optionalEvaluate conditions:
geq: evaluate a>=condition. (Default)g: evaluate a>condition.seq: evaluate a<=condition.s: evaluate a<condition.eq: evaluate a==condition.neq: evaluate a!=condition.
reuse : bool
Set to True to add a reverse circuit part to reuse ancilla qubits.
- Returns
circuit :
QCircuitThe result this function return is a quangtum circuit. The comparison result qubit would be in state \(|1\rangle\) when the quantum state satisfies the comparison condition, otherwise \(|0\rangle\). Therefore we can get a probability outcome of the comparison.
- Examples
An example of implementing a two-qubit uniform superposition state compared with qubit state ‘01’.
>>> from pyqpanda_alg import QCmp >>> import numpy as np >>> from pyqpanda3.core import * >>> m = CPUQVM() >>> prog = QProg() >>> prog << H(0) << H(1) << X(3) >>> cir = QCmp.qubit_comparator([0, 1], [2, 3], [4, 5], function='g') >>> prog << cir >>> m.run(prog, 1000) >>> prob_dict_result = m.result().get_prob_dict([5]) >>> print(prob_dict_result) {'0': 0.7500000000000003, '1': 0.2500000000000001}
- pyqpanda_alg.QCmp.qft_comparator(value, q_state, q_cmp, function='geq')¶
This function provides qft_based comparators to compare basis states(can be superposition states) against a given classical integer.
- Parameters
value :
intThe given classical integer in range [0,N).
q_state :
Qubit,QVecState qubits.
q_cmp :
QVecThe comparison result qubit.
function :
str{'geq', 'g', 'seq', 's'}, optionalEvaluate conditions:
geq: evaluate a>=condition. (Default)g: evaluate a>condition.seq: evaluate a<=condition.s: evaluate a<condition.
- Returns
circuit :
QCircuitThe result this function return is a quangtum circuit. The comparison result qubit would be in state \(|1\rangle\) when the quantum state satisfies the comparison condition, otherwise \(|0\rangle\). Therefore we can get a probability outcome of the comparison.
- Examples
An example of implementing a two-qubit uniform superposition state compared with 2.
>>> from pyqpanda_alg import QCmp >>> import numpy as np >>> from pyqpanda3.core import * >>> value = 2 >>> m = CPUQVM() >>> prog = QProg() >>> prog << H(0) << H(1) >>> cir = QCmp.qft_comparator(value, [0, 1], [2], function='g') >>> prog << cir >>> m.run(prog, 1000) >>> prob_dict_result = m.result().get_prob_dict([2]) >>> print(prob_dict_result) {'0': 0.750000000000001, '1': 0.2500000000000002}
- pyqpanda_alg.QCmp.qft_qubit_comparator(q_state_1, q_state_2, q_cmp, function='geq')¶
This function provides qft_based comparators to compare between two basis states(can be superposition states).
- Parameters
q_state_1 :
Qubit,QVecThe first state qubits.
q_state_2 :
Qubit,QVecThe second state qubits.
q_cmp :
QVecComparison result qubit.
function :
str{'geq', 'g', 'seq', 's'}, optionalEvaluate conditions:
geq: evaluate a>=condition. (Default)g: evaluate a>condition.seq: evaluate a<=condition.s: evaluate a<condition.
- Returns
circuit :
QCircuitThe result this function return is a quangtum circuit. The comparison result qubit would be in state \(|1\rangle\) when the quantum state satisfies the comparison condition, otherwise \(|0\rangle\). Therefore we can get a probability outcome of the comparison.
- Examples
An example of implementing a two-qubit uniform superposition state compared with qubit state ‘01’.
>>> from pyqpanda_alg import QCmp >>> import numpy as np >>> from pyqpanda3.core import * >>> m = CPUQVM() >>> prog = QProg() >>> prog << H(0) << H(1) << X(3) >>> cir = QCmp.qft_qubit_comparator([0, 1], [2, 3], [4], function='g') >>> prog << cir >>> m.run(prog, 1000) >>> prob_dict_result = m.result().get_prob_dict([4]) >>> print(prob_dict_result) {'0': 0.7500000000000007, '1': 0.2500000000000002}