Skip to content

Commit

Permalink
added qiskit installation to workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
claretgrace0801 committed Jul 25, 2022
1 parent 412c15c commit 7369167
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 38 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,19 @@ jobs:
include:
- os: ubuntu-latest
qutip-version: '@dev.major'
qiskit-version: ''
python-version: '3.10'
- os: windows-latest
qutip-version: '==4.6.*'
qiskit-version: ''
python-version: '3.8'
- os: macOS-latest
qutip-version: '==4.7.*'
qiskit-version: ''
python-version: '3.9'
- os: ubuntu-latest
qutip-version: ''
qiskit-version: '==0.36.*'
python-version: '3.7'

steps:
Expand All @@ -41,6 +45,10 @@ jobs:
python -m pip install numpy scipy cython
python -m pip install 'git+https://github.com/qutip/qutip.git${{ matrix.qutip-version }}'
- name: Install Qiskit from PyPI
if: ${{ matrix.qiskit-version != '' }}
run: python -m pip install 'qiskit${{ matrix.qiskit-version }}'

- name: Install qutip-qip
# Installing in-place so that coveralls can locate the source code.
run: |
Expand Down
25 changes: 15 additions & 10 deletions src/qutip_qip/qiskit/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ def run(self, qiskit_circuit: QuantumCircuit, **backend_options) -> Job:
backend=self,
job_id=job_id,
result=self._run_job(job_id, qutip_circ),
warnings=qutip_circ._warnings if hasattr(
qutip_circ, "_warnings") else []
warnings=qutip_circ._warnings
if hasattr(qutip_circ, "_warnings")
else [],
)
return job

Expand Down Expand Up @@ -141,14 +142,18 @@ def convert_to_hex(count):
counts=counts, statevector=Statevector(data=np.array(statevector))
)

header = QobjExperimentHeader.from_dict({
"name": qutip_circuit.name if hasattr(
qutip_circuit, "name") else "",
"n_qubits": qutip_circuit.N,
})
header = QobjExperimentHeader.from_dict(
{
"name": qutip_circuit.name
if hasattr(qutip_circuit, "name")
else "",
"n_qubits": qutip_circuit.N,
}
)

exp_res = ExperimentResult(shots=1, success=True, data=exp_res_data,
header=header)
exp_res = ExperimentResult(
shots=1, success=True, data=exp_res_data, header=header
)

result = Result(
backend_name=self._configuration["backend_name"],
Expand Down Expand Up @@ -179,7 +184,7 @@ def _run_job(self, job_id: str, qutip_circuit: QubitCircuit) -> Result:
qiskit.result.Result
Result of the simulation
"""
zero_state = basis([2]*qutip_circuit.N, [0]*qutip_circuit.N)
zero_state = basis([2] * qutip_circuit.N, [0] * qutip_circuit.N)
statistics = qutip_circuit.run_statistics(state=zero_state)

return self._parse_results(
Expand Down
10 changes: 4 additions & 6 deletions src/qutip_qip/qiskit/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,8 @@ def _get_qutip_index(bit_index: Union[int, list], total_bits: int) -> int:
"""

if type(bit_index) == type([]):
return [
_get_qutip_index(bit, total_bits)
for bit in bit_index
]
if isinstance(bit_index, list):
return [_get_qutip_index(bit, total_bits) for bit in bit_index]
else:
return total_bits - 1 - bit_index

Expand Down Expand Up @@ -144,7 +141,8 @@ def convert_qiskit_circuit(qiskit_circuit: QuantumCircuit) -> QubitCircuit:
# The 0th bit is the control bit in qiskit by
# convention, in case of a controlled operation
controls=_get_mapped_bits(
[qiskit_qregs[0]], bit_map=qubit_map),
[qiskit_qregs[0]], bit_map=qubit_map
),
targets=_get_mapped_bits(qiskit_qregs[1:], bit_map=qubit_map),
arg_value=arg_value,
)
Expand Down
51 changes: 29 additions & 22 deletions tests/test_qiskit.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
from qiskit import QuantumCircuit
from qiskit.providers.aer import AerSimulator
from qutip_qip.qiskit import Provider
from qutip_qip.qiskit.converter import convert_qiskit_circuit, _get_qutip_index
except:
from qutip_qip.qiskit.converter import (
convert_qiskit_circuit,
_get_qutip_index,
)
except ImportError:
pass


Expand All @@ -26,20 +29,26 @@ class TestConverter:
def _compare_args(self, req_gate, res_gate):
"""Compare parameters of two gates"""
res_arg = (
res_gate.arg_value if type(
(
res_gate.arg_value
) is list or type(
res_gate.arg_value
) is tuple else [res_gate.arg_value]
) if res_gate.arg_value else []
if type(res_gate.arg_value) is list
or type(res_gate.arg_value) is tuple
else [res_gate.arg_value]
)
if res_gate.arg_value
else []
)

req_arg = (
req_gate.arg_value if type(
req_gate.arg_value
) is list or type(
(
req_gate.arg_value
) is tuple else [req_gate.arg_value]
) if req_gate.arg_value else []
if type(req_gate.arg_value) is list
or type(req_gate.arg_value) is tuple
else [req_gate.arg_value]
)
if req_gate.arg_value
else []
)

if len(req_arg) != len(res_arg):
return False
Expand All @@ -56,11 +65,8 @@ def _compare_gate(self, req_gate, res_gate, result_circuit: QubitCircuit):
return False

if req_gate.name == "measure":
check_condition = (
req_gate.classical_store
== _get_qutip_index(
res_gate.classical_store, result_circuit.num_cbits
)
check_condition = req_gate.classical_store == _get_qutip_index(
res_gate.classical_store, result_circuit.num_cbits
)
else:
# todo: correct for float error in arg_value
Expand All @@ -71,8 +77,9 @@ def _compare_gate(self, req_gate, res_gate, result_circuit: QubitCircuit):
)
req_controls = req_gate.controls if req_gate.controls else None

check_condition = (res_controls == req_controls) and self._compare_args(
req_gate, res_gate)
check_condition = (
res_controls == req_controls
) and self._compare_args(req_gate, res_gate)

return check_condition

Expand Down Expand Up @@ -148,13 +155,13 @@ def _compare_results(self, qiskit_circuit: QuantumCircuit):
qutip_backend = provider.get_backend("circuit_simulator")
qutip_job = qutip_backend.run(qiskit_circuit)
qutip_result = qutip_job.result()
qutip_sv = qutip_result.data()['statevector']
qutip_sv = qutip_result.data()["statevector"]

qiskit_backend = AerSimulator(method='statevector')
qiskit_backend = AerSimulator(method="statevector")
qiskit_circuit.save_state()
qiskit_job = qiskit_backend.run(qiskit_circuit)
qiskit_result = qiskit_job.result()
qiskit_sv = qiskit_result.data()['statevector']
qiskit_sv = qiskit_result.data()["statevector"]

assert_allclose(qutip_sv, qiskit_sv)

Expand Down

0 comments on commit 7369167

Please sign in to comment.