Skip to content

Commit f7a4a34

Browse files
add quoperator method for circuit
1 parent f5bd68f commit f7a4a34

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
### Added
6+
7+
- Add `quoperator` method to get `QuOperator` representation of the circuit unitary
8+
59
## 0.1.0
610

711
### Added

tensorcircuit/circuit.py

+17
Original file line numberDiff line numberDiff line change
@@ -1216,6 +1216,23 @@ def _copy_state_tensor(
12161216

12171217
state = wavefunction
12181218

1219+
def get_quoperator(self) -> QuOperator:
1220+
"""
1221+
Get the ``QuOperator`` MPO like representation of the circuit unitary without contraction.
1222+
1223+
:return: ``QuOperator`` object for the circuit unitary (open indices for the input state)
1224+
:rtype: QuOperator
1225+
"""
1226+
mps = identity([2 for _ in range(self._nqubits)])
1227+
c = Circuit(self._nqubits)
1228+
ns, es = self._copy()
1229+
c._nodes = ns
1230+
c._front = es
1231+
c.replace_mps_inputs(mps)
1232+
return QuOperator(c._front[: self._nqubits], c._front[self._nqubits :])
1233+
1234+
quoperator = get_quoperator
1235+
12191236
def matrix(self) -> Tensor:
12201237
"""
12211238
Get the unitary matrix for the circuit irrespective with the circuit input state.

tests/test_circuit.py

+14
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,20 @@ def test_apply_multicontrol_gate():
815815
np.testing.assert_allclose(c.expectation([tc.gates.z(), [3]]), -1, atol=1e-5)
816816

817817

818+
@pytest.mark.parametrize("backend", [lf("npb"), lf("tfb"), lf("jaxb")])
819+
def test_circuit_quoperator(backend):
820+
c = tc.Circuit(3)
821+
c.x(0)
822+
c.cnot(0, 1)
823+
c.cz(1, 2)
824+
c.y(2)
825+
c.exp1(0, 2, theta=1.0, unitary=tc.gates._xx_matrix)
826+
c.H(1)
827+
c.multicontrol(0, 2, 1, ctrl=[1, 0], unitary=tc.gates.z())
828+
qo = c.quoperator()
829+
np.testing.assert_allclose(qo.eval_matrix(), c.matrix(), atol=1e-5)
830+
831+
818832
@pytest.mark.parametrize("backend", [lf("npb"), lf("tfb"), lf("jaxb")])
819833
def test_qir2qiskit(backend):
820834
try:

0 commit comments

Comments
 (0)