Skip to content

Commit 4bf70b6

Browse files
add to_openqasm
1 parent 8193f0f commit 4bf70b6

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

Diff for: CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
- Add new parameter shift gradient API that supports finite measurement shots and the corresponding example scripts
88

9+
- Add openqasm format transformation method `c.to_openqasm()`
10+
911
### Changed
1012

1113
- The inner mechanism for `sample_expectation_ps` is changed to sample representation from count representation for a fast speed

Diff for: tensorcircuit/abstractcircuit.py

+11
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,17 @@ def to_qiskit(self) -> Any:
509509
qir = self.to_qir()
510510
return qir2qiskit(qir, n=self._nqubits)
511511

512+
def to_openqasm(self, **kws: Any) -> str:
513+
"""
514+
transform circuit to openqasm via qiskit circuit,
515+
see https://qiskit.org/documentation/stubs/qiskit.circuit.QuantumCircuit.qasm.html
516+
for usage on possible options for ``kws``
517+
518+
:return: circuit representation in openqasm format
519+
:rtype: str
520+
"""
521+
return self.to_qiskit().qasm(**kws) # type: ignore
522+
512523
def draw(self, **kws: Any) -> Any:
513524
"""
514525
Visualise the circuit.

Diff for: tests/test_circuit.py

+10
Original file line numberDiff line numberDiff line change
@@ -1193,3 +1193,13 @@ def test_gate_count():
11931193
assert c.gate_count(["rx", "multicontrol"]) == 2
11941194
print(c.gate_summary())
11951195
# {'h': 2, 'rx': 1, 'multicontrol': 1, 'toffoli': 3}
1196+
1197+
1198+
def test_to_openqasm():
1199+
c = tc.Circuit(3)
1200+
c.H(0)
1201+
c.rz(2, theta=0.2)
1202+
c.cnot(2, 1)
1203+
c.rzz(0, 1, theta=-1.0)
1204+
c.ccx(1, 2, 0)
1205+
print(c.to_openqasm(formatted=True))

0 commit comments

Comments
 (0)