pyqpanda_alg.QLuoShu.lshift
¶
Module Contents¶
Functions¶
|
Quantum Circuit of A binary Doubling Operation \(*2\). |
- pyqpanda_alg.QLuoShu.lshift.lshift(qvec)¶
Quantum Circuit of A binary Doubling Operation \(*2\).
- Parameters:
qvec:
qlist
the qubits list
- Return:
circuit:
pq.QCircuit
The circuit is by the SWAP operation to shift the qubits in the quantum registers to the left sequentially by bits. It works in place with an ancilla qubit that compute \(|x \rangle \rightarrow |2x \rangle\). Therefore, it uses only \(n+1\) qubits with \(n=\lceil \log_{2}x \rceil\).
- Example:
If \(x=13\), putting \(x\) held in \(|qvec \rangle\). By the circuit, the result \(|11010 \rangle\) will be held in the \(|qvec \rangle\), i.e., \(2x=26\).
from pyqpanda import * import math from pyqpanda_alg.QLuoShu import lshift if __name__ == "__main__": x = 13 n = math.ceil(math.log(x, 2)) + 1 qvm = init_quantum_machine(QMachineType.CPU) prog = QProg() qvec = qvm.qAlloc_many(n) prog << bind_nonnegative_data(x, qvec) \ << lshift.lshift(qvec) result = prob_run_dict(prog, qvec, 1) for key in result: print(key + ":" + str(result[key])) c = int(key, 2) print("2*%d=%d" % (x, c))
11010:1.0 2*13=26
Note that: if \(x\) is a power of 2, we need to let \(\lceil \log_{2}x \rceil+2\) qubits.
Here, we give the circuit graph of the example in the above with 5 qubits in the following:
q_0: |0>── ─ ─ X │ q_1: |0>── ─ X X │ q_2: |0>── X X ─ │ q_3: |0>─X X ─ ─ │ q_4: |0>─X ─ ─ ─