Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed Deprecated Instruction.qasm Function #11538

Merged
merged 10 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions qiskit/circuit/controlflow/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,6 @@ def _copy_mutable_properties(self, instruction: Instruction) -> Instruction:
def assemble(self):
raise CircuitError("Cannot assemble a placeholder instruction.")

def qasm(self):
raise CircuitError("Cannot convert a placeholder instruction to OpenQASM 2")

def repeat(self, n):
raise CircuitError("Cannot repeat a placeholder instruction.")

Expand Down
27 changes: 1 addition & 26 deletions qiskit/circuit/instruction.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@
from qiskit.qobj.qasm_qobj import QasmQobjInstruction
from qiskit.circuit.parameter import ParameterExpression
from qiskit.circuit.operation import Operation
from qiskit.utils.deprecation import deprecate_func
from .tools import pi_check


_CUTOFF_PRECISION = 1e-10

Expand Down Expand Up @@ -516,30 +515,6 @@ def _qasmif(self, string):
)
return "if(%s==%d) " % (self.condition[0].name, self.condition[1]) + string

@deprecate_func(
additional_msg=(
"Correct exporting to OpenQASM 2 is the responsibility of a larger exporter; it cannot "
"safely be done on an object-by-object basis without context. No replacement will be "
"provided, because the premise is wrong."
),
since="0.25.0",
package_name="qiskit-terra",
)
def qasm(self):
"""Return a default OpenQASM string for the instruction.

Derived instructions may override this to print in a
different format (e.g. ``measure q[0] -> c[0];``).
"""
name_param = self.name
if self.params:
name_param = "{}({})".format(
name_param,
",".join([pi_check(i, output="qasm", eps=1e-12) for i in self.params]),
)

return self._qasmif(name_param)

def broadcast_arguments(self, qargs, cargs):
"""
Validation of the arguments.
Expand Down
9 changes: 0 additions & 9 deletions qiskit/circuit/library/hamiltonian_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
from qiskit.circuit.exceptions import CircuitError
from qiskit.quantum_info.operators.predicates import matrix_equal
from qiskit.quantum_info.operators.predicates import is_hermitian_matrix
from qiskit.utils.deprecation import deprecate_func

from .generalized_gates.unitary import UnitaryGate

Expand Down Expand Up @@ -128,14 +127,6 @@ def _define(self):
qc._append(UnitaryGate(self.to_matrix()), q[:], [])
self.definition = qc

@deprecate_func(
since="0.25.0",
package_name="qiskit-terra",
)
def qasm(self):
"""Raise an error, as QASM is not defined for the HamiltonianGate."""
raise CircuitError("HamiltonianGate has no OpenQASM 2 definition.")

def validate_parameter(self, parameter):
"""Hamiltonian parameter has to be an ndarray, operator or float."""
if isinstance(parameter, (float, int, np.ndarray)):
Expand Down
19 changes: 0 additions & 19 deletions qiskit/circuit/library/standard_gates/x.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from typing import Optional, Union, Type
from math import ceil, pi
import numpy
from qiskit.utils.deprecation import deprecate_func
from qiskit.circuit.controlledgate import ControlledGate
from qiskit.circuit.singleton import SingletonGate, SingletonControlledGate, stdlib_singleton_key
from qiskit.circuit.quantumregister import QuantumRegister
Expand Down Expand Up @@ -590,24 +589,6 @@ def _define(self):

self.definition = qc

@deprecate_func(since="0.25.0", package_name="qiskit-terra")
def qasm(self):
# Gross hack to override the Qiskit name with the name this gate has in Terra's version of
# 'qelib1.inc'. In general, the larger exporter mechanism should know about this to do the
# mapping itself, but right now that's not possible without a complete rewrite of the OQ2
# exporter code (low priority), or we would need to modify 'qelib1.inc' which would be
# needlessly disruptive this late in OQ2's lifecycle. The current OQ2 exporter _always_
# outputs the `include 'qelib1.inc' line. ---Jake, 2022-11-21.
old_name = self.name
if not self.mutable:
copy_self = self.to_mutable()
copy_self.name = "c3sqrtx"
return copy_self.qasm()
try:
return super().qasm()
finally:
self.name = old_name


@with_controlled_gate_array(_X_ARRAY, num_ctrl_qubits=3, cached_states=(7,))
class C3XGate(SingletonControlledGate):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
upgrade:
- |
Removed the ``Instruction.qasm`` method, which was deprecated in Qiskit 0.45.0.
Use :func:`qiskit.qasm2.dump` with a complete :class:`.QuantumCircuit` instead.
5 changes: 0 additions & 5 deletions test/python/circuit/test_circuit_qasm.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,6 @@ def test_c3sxgate_roundtrips(self):
parsed = QuantumCircuit.from_qasm_str(qasm)
self.assertIsInstance(parsed.data[0].operation, C3SXGate)

def test_c3sxgate_qasm_deprecation_warning(self):
"""Test deprecation warning for C3SXGate."""
with self.assertWarnsRegex(DeprecationWarning, r"Correct exporting to OpenQASM 2"):
C3SXGate().qasm()

def test_cczgate_qasm(self):
"""Test that CCZ dumps definition as a non-qelib1 gate."""
qc = QuantumCircuit(3)
Expand Down
9 changes: 0 additions & 9 deletions test/python/circuit/test_hamiltonian_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
from qiskit.circuit import Parameter
from qiskit.quantum_info import Operator
from qiskit.converters import circuit_to_dag, dag_to_circuit
from qiskit.circuit.exceptions import CircuitError


class TestHamiltonianGate(QiskitTestCase):
Expand Down Expand Up @@ -89,14 +88,6 @@ def test_1q_hamiltonian(self):
self.assertEqual(dnode.qargs, tuple(qc.qubits))
assert_allclose(dnode.op.to_matrix(), np.eye(2))

def test_error_and_deprecation_warning_on_qasm(self):
"""test that an error is thrown if the method `qasm` is called."""
matrix = np.zeros((2, 2))
hamiltonian_gate = HamiltonianGate(data=matrix, time=1)
with self.assertRaises(CircuitError):
with self.assertWarns(DeprecationWarning):
hamiltonian_gate.qasm()

def test_2q_hamiltonian(self):
"""test 2 qubit hamiltonian"""
qr = QuantumRegister(2)
Expand Down
9 changes: 0 additions & 9 deletions test/python/circuit/test_instructions.py
Original file line number Diff line number Diff line change
Expand Up @@ -655,15 +655,6 @@ def test_label_type_enforcement(self):
instruction = RZGate(0)
instruction.label = 0

def test_deprecation_warnings_qasm_methods(self):
"""Test deprecation warnings for qasm methods."""
with self.subTest("built in gates"):
with self.assertWarnsRegex(DeprecationWarning, r"Correct exporting to OpenQASM 2"):
HGate().qasm()
with self.subTest("User constructed Instruction"):
with self.assertWarnsRegex(DeprecationWarning, r"Correct exporting to OpenQASM 2"):
Instruction("v", 1, 0, [0.4, 0.5, 0.5]).qasm()


if __name__ == "__main__":
unittest.main()
3 changes: 2 additions & 1 deletion test/python/transpiler/test_mappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def test_a_common_test(self):

from qiskit import execute
from qiskit import ClassicalRegister, QuantumRegister, QuantumCircuit, BasicAer
from qiskit.qasm2 import dump
from qiskit.transpiler import PassManager
from qiskit.transpiler.passes import BasicSwap, LookaheadSwap, StochasticSwap, SabreSwap
from qiskit.transpiler.passes import SetLayout
Expand Down Expand Up @@ -132,7 +133,7 @@ def generate_ground_truth(self, transpiled_result, filename):
)
self.assertDictAlmostEqual(self.counts, job.result().get_counts(), delta=self.delta)

transpiled_result.qasm(formatted=False, filename=filename)
dump(transpiled_result.qasm(formatted=False), filename)

def assertResult(self, result, circuit):
"""Fetches the QASM in circuit.name file and compares it with result."""
Expand Down