Giter Club home page Giter Club logo

qiskit-qir's Introduction

qiskit-qir

Qiskit to QIR translator.

Example

from qiskit import QuantumCircuit
from qiskit_qir import to_qir_module

circuit = QuantumCircuit(3, 3, name="my-circuit")
circuit.h(0)
circuit.cx(0, 1)
circuit.cx(1, 2)
circuit.measure([0,1,2], [0, 1, 2])

module, entry_points = to_qir_module(circuit)
bitcode = module.bitcode
ir = str(module)

Installation

Install qiskit-qir with pip:

pip install qiskit-qir

Note: this will automatically install PyQIR if needed.

Development

Install from source

To install the package from source, clone the repo onto your machine, browse to the root directory and run

pip install -e .

Tests

First, install the development dependencies using

pip install -r requirements_dev.txt

To run the tests in your local environment, run

make test

To run the tests in virtual environments on supported Python versions, run

make test-all

Docs

To build the docs using Sphinx, run

make docs

qiskit-qir's People

Contributors

anpaz avatar dependabot[bot] avatar idavis avatar jrj-d avatar microsoft-github-policy-service[bot] avatar ricardo-espinoza avatar swernli avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

qiskit-qir's Issues

qiskit_qir fails to parse standard rotation gates

  • qiskit-qir version: 0.3.1
  • Python version: 3.10
  • Operating System: macOS Monterey 12.6.3

Description

I encountered an issue with the standard rotation gates (MWE 1.) I noticed the only way to make it work is by truncating the angle to 5 digits after the decimal point (MWE 2.)

MWE 1:

from qiskit import QuantumCircuit
from qiskit_qir import to_qir_module
from math import pi

qc = QuantumCircuit(1)
qc.rx(pi, 0)
mod, _ = to_qir_module(qc)
print(mod)

Output:

call void @__quantum__qis__rx__body(double 0x400921FB54442D18, %Qubit* null)

MWE 2:

from qiskit import QuantumCircuit
from qiskit_qir import to_qir_module
from math import pi

qc = QuantumCircuit(1)
qc.rx(round(pi, 5), 0)
mod, _ = to_qir_module(qc)
print(mod)

Output:

call void @__quantum__qis__rx__body(double 3.141590e+00, %Qubit* null)

qiskit-qir fails on preparing a 0 state with circuit.initialize([1, 0], 0)

  • qiskit-qir version: 0.3.1
  • Python version: 3.9
  • Operating System: Linux Debian bullseye

Description

I tried to compile a Qiskit circuit to QIR and it failed.

What I Did

from qiskit import QuantumCircuit
from qiskit_qir import to_qir_module

circuit = QuantumCircuit(1, 0)
circuit.initialize([1, 0], 0)  # P0
circuit.draw()
   ┌─────────────────┐
q: ┤ Initialize(1,0) ├
   └─────────────────┘
to_qir_module(circuit)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[8], line 1
----> 1 to_qir_module(circuit)

File ~/ab/demos/20230404/venv/lib/python3.9/site-packages/qiskit_qir/translate.py:60, in to_qir_module(circuits, profile, **kwargs)
     58     module = QiskitModule.from_quantum_circuit(circuit, llvm_module)
     59     visitor = BasicQisVisitor(profile, **kwargs)
---> 60     module.accept(visitor)
     61     entry_points.append(visitor.entry_point)
     62 err = llvm_module.verify()

File ~/ab/demos/20230404/venv/lib/python3.9/site-packages/qiskit_qir/elements.py:104, in QiskitModule.accept(self, visitor)
    102 visitor.visit_qiskit_module(self)
    103 for element in self._elements:
--> 104     element.accept(visitor)
    105 visitor.record_output(self)
    106 visitor.finalize()

File ~/ab/demos/20230404/venv/lib/python3.9/site-packages/qiskit_qir/elements.py:38, in _Instruction.accept(self, visitor)
     37 def accept(self, visitor):
---> 38     visitor.visit_instruction(self._instruction, self._qargs, self._cargs)

File ~/ab/demos/20230404/venv/lib/python3.9/site-packages/qiskit_qir/visitor.py:355, in BasicQisVisitor.visit_instruction(self, instruction, qargs, cargs, skip_condition)
    351         elif instruction.definition:
    352             _log.debug(
    353                 f"About to process composite instruction {instruction.name} with qubits {qargs}"
    354             )
--> 355             self.process_composite_instruction(instruction, qargs, cargs)
    356         else:
    357             raise ValueError(
    358                 f"Gate {instruction.name} is not supported. \
    359 Please transpile using the list of supported gates: {_SUPPORTED_INSTRUCTIONS}."
    360             )

File ~/ab/demos/20230404/venv/lib/python3.9/site-packages/qiskit_qir/visitor.py:206, in BasicQisVisitor.process_composite_instruction(self, instruction, qargs, cargs)
    202 mapped_clbits = [cargs[subcircuit.clbits.index(i)] for i in i_cargs]
    203 _log.debug(
    204     f"Processing sub-instruction {inst.name} with mapped qubits {mapped_qbits}"
    205 )
--> 206 self.visit_instruction(inst, mapped_qbits, mapped_clbits)

File ~/ab/demos/20230404/venv/lib/python3.9/site-packages/qiskit_qir/visitor.py:355, in BasicQisVisitor.visit_instruction(self, instruction, qargs, cargs, skip_condition)
    351         elif instruction.definition:
    352             _log.debug(
    353                 f"About to process composite instruction {instruction.name} with qubits {qargs}"
    354             )
--> 355             self.process_composite_instruction(instruction, qargs, cargs)
    356         else:
    357             raise ValueError(
    358                 f"Gate {instruction.name} is not supported. \
    359 Please transpile using the list of supported gates: {_SUPPORTED_INSTRUCTIONS}."
    360             )

File ~/ab/demos/20230404/venv/lib/python3.9/site-packages/qiskit_qir/visitor.py:206, in BasicQisVisitor.process_composite_instruction(self, instruction, qargs, cargs)
    202 mapped_clbits = [cargs[subcircuit.clbits.index(i)] for i in i_cargs]
    203 _log.debug(
    204     f"Processing sub-instruction {inst.name} with mapped qubits {mapped_qbits}"
    205 )
--> 206 self.visit_instruction(inst, mapped_qbits, mapped_clbits)

File ~/ab/demos/20230404/venv/lib/python3.9/site-packages/qiskit_qir/visitor.py:357, in BasicQisVisitor.visit_instruction(self, instruction, qargs, cargs, skip_condition)
    355             self.process_composite_instruction(instruction, qargs, cargs)
    356         else:
--> 357             raise ValueError(
    358                 f"Gate {instruction.name} is not supported. \
    359 Please transpile using the list of supported gates: {_SUPPORTED_INSTRUCTIONS}."
    360             )

ValueError: Gate disentangler_dg is not supported.     Please transpile using the list of supported gates: ['barrier', 'ccx', 'cx', 'cz', 'h', 'id', 'm', 'measure', 'reset', 'rx', 'ry', 'rz', 's', 'sdg', 'swap', 't', 'tdg', 'x', 'y', 'z', 'delay'].

Short investigation

I suppose this happens because the disentangler_dg is contained within the initialize command above:

circuit.decompose().decompose().draw()
        ┌─────────────────┐
q: ─|0>─┤ disentangler_dg ├
        └─────────────────┘

Unfortunately, I'm not familiar with this gate and have a hard time tracking it in the code of qiskit.

Additionally, it is worth noting that the other basis states are compiled just fine:

  • 1: [0, 1]
  • +: [1.0 / np.sqrt(2), 1.0 / np.sqrt(2)]
  • -: [1.0 / np.sqrt(2), -1.0 / np.sqrt(2)]

qiskit_qir fails to parse identity gates

  • qiskit-qir version: 0.3.1
  • Python version: 3.10
  • Operating System: macOS Monterey 12.6.3

Description

qiskit-qir places two not gates in the first qubit whenever there are identity gates in the circuit disregarding their actual placement.

MWE:

from qiskit import QuantumCircuit
from qiskit_qir import to_qir_module

qc = QuantumCircuit(2)
qc.id(0)
qc.id(1)
mod, _ = to_qir_module(qc)
print(mod)

Output:

call void @__quantum__qis__x__body(%Qubit* null)
call void @__quantum__qis__x__body(%Qubit* null)
call void @__quantum__qis__x__body(%Qubit* null)
call void @__quantum__qis__x__body(%Qubit* null)

From visitor.py:

elif "id" == instruction.name:
    # See: https://github.com/qir-alliance/pyqir/issues/74
    qis.x(self._builder, pyqir.qubit(self._module.context, 0))
    qis.x(self._builder, pyqir.qubit(self._module.context, 0))

Native support for Qiskit `ccx` gate

  • qiskit-qir version: 0.1.0b10
  • Python version: 3.7.13

Description

It would be great if qiskit-qir directly supports Qiskit's ccx instruction without breaking it down into other instructions here:

elif instruction.definition:
_log.debug(
f"About to process composite instruction {instruction.name} with qubits {qargs}")
self.process_composite_instruction(instruction, qargs, cargs)

Update `pyqir` dependecy to support macOS with M1 processors

  • qiskit-qir version: 0.2.0 (and today's main)
  • Python version: 3.8
  • Operating System: macOS with an ARM processor (M1)

Description

Following qir-alliance/pyqir#101 (comment) and pyqir v0.7.0 release, qiskit-qir can support macOS with M1 processors.
To achieve this, the fixed requirement on pyqir has to be updated or relaxed:

pyqir-generator == 0.4.2a1


Also, it's probably desirable to replace pyqir-generator with pyqir:

pyqir-generator has been replaced by pyqir

Please install the pyqir package instead, which provides the same functionality.
Read the upgrade guide for help with upgrading from pyqir-generator 0.6.

https://pypi.org/project/pyqir-generator/

preserve `barrier` in QIR

Description

Currently, barrier is a no-op:

if "barrier" == instruction.name:
pass

However, barrier is required for many kinds of characterization circuits. For example, without a barrier instruction, randomized benchmarking would collapse into a no-op! By removing it here, the user's requirements are lost.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.