|
| 1 | +""" |
| 2 | +i |
| 3 | +MIT License |
| 4 | +
|
| 5 | +Copyright (c) 2020-present TorchQuantum Authors |
| 6 | +
|
| 7 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +of this software and associated documentation files (the "Software"), to deal |
| 9 | +in the Software without restriction, including without limitation the rights |
| 10 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +copies of the Software, and to permit persons to whom the Software is |
| 12 | +furnished to do so, subject to the following conditions: |
| 13 | +
|
| 14 | +The above copyright notice and this permission notice shall be included in all |
| 15 | +copies or substantial portions of the Software. |
| 16 | +
|
| 17 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | +SOFTWARE. |
| 24 | +""" |
| 25 | + |
| 26 | +from cuquantum import contract |
| 27 | +from cuquantum import CircuitToEinsum |
| 28 | +import torchquantum as tq |
| 29 | +from torchquantum.measurement import expval_joint_analytical |
| 30 | +import cupy as cp |
| 31 | + |
| 32 | +def expval_joint_analytical_cuquantum(qdev, observable): |
| 33 | + """Computes the expectation value of a joint observable using cuquantum. |
| 34 | +
|
| 35 | + Args: |
| 36 | + qdev (QuantumDevice): Quantum device to compute the expectation value on. |
| 37 | + observable (str): Joint observable to compute the expectation value of. |
| 38 | +
|
| 39 | + Returns: |
| 40 | + float: The expectation value of the joint observable. |
| 41 | + """ |
| 42 | + op_history = qdev.op_history |
| 43 | + myconverter = CircuitToEinsum(qdev, dtype='complex128', backend=cp) |
| 44 | + expression, operands = myconverter.expectation(observable, lightcone=True) |
| 45 | + expec = contract(expression, *operands) |
| 46 | + return expec |
| 47 | + |
| 48 | + |
| 49 | +if __name__ == '__main__': |
| 50 | + |
| 51 | + ops = [ |
| 52 | + {'name': 'u3', 'wires': 0, 'trainable': True}, |
| 53 | + {'name': 'u3', 'wires': 1, 'trainable': True}, |
| 54 | + {'name': 'cx', 'wires': [0, 1]}, |
| 55 | + {'name': 'cx', 'wires': [1, 0]}, |
| 56 | + {'name': 'u3', 'wires': 0, 'trainable': True}, |
| 57 | + {'name': 'u3', 'wires': 1, 'trainable': True}, |
| 58 | + {'name': 'cx', 'wires': [0, 1]}, |
| 59 | + {'name': 'cx', 'wires': [1, 0]}, |
| 60 | + ] |
| 61 | + |
| 62 | + qmodule = tq.QuantumModule.from_op_history(ops) |
| 63 | + |
| 64 | + qdev = tq.QuantumDevice(n_wires=2, bsz=1, record_op=True) |
| 65 | + |
| 66 | + qmodule(qdev) |
| 67 | + |
| 68 | + op_history = qdev.op_history |
| 69 | + |
| 70 | + print(qdev.op_history) |
| 71 | + |
| 72 | + myconverter = CircuitToEinsum(qdev, dtype='complex128', backend=cp) |
| 73 | + pauli_string = 'IX' |
| 74 | + expression, operands = myconverter.expectation(pauli_string, lightcone=True) |
| 75 | + expec = contract(expression, *operands) |
| 76 | + print(f'expectation value for {pauli_string}: {expec}') |
| 77 | + |
| 78 | + print(f"torchquantum expval: {expval_joint_analytical(qdev, pauli_string)}") |
| 79 | + print(expval_joint_analytical_cuquantum(qdev, pauli_string)) |
| 80 | + |
| 81 | + |
| 82 | + # # expectation value from reduced density matrix |
| 83 | + # qubits = myconverter.qubits |
| 84 | + # where = qubits[1:5] |
| 85 | + # rdm_expression, rdm_operands = myconverter.reduced_density_matrix(where, lightcone=True) |
| 86 | + # rdm = contract(rdm_expression, *rdm_operands) |
| 87 | + |
| 88 | + # pauli_x = cp.asarray([[0,1],[1,0]], dtype=myconverter.dtype) |
| 89 | + # pauli_z = cp.asarray([[1,0],[0,-1]], dtype=myconverter.dtype) |
| 90 | + # expec_from_rdm = cp.einsum('abcdABCD,aA,bB,cC,dD->', rdm, pauli_x, pauli_x, pauli_z, pauli_z) |
| 91 | + |
| 92 | + |
| 93 | + # print(f"is expectation value in agreement?", cp.allclose(expec, expec_from_rdm)) |
| 94 | + |
0 commit comments