-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
75 changed files
with
3,976 additions
and
549 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
from mqt.qudits.qudit_circuits.circuit import QuantumCircuit | ||
from mqt.qudits.qudit_circuits.components.registers.quantum_register import QuantumRegister | ||
from mqt.qudits.simulation.provider.qudit_provider import MQTQuditProvider | ||
from mqt.qudits.visualisation.plot_information import plot_counts, plot_state | ||
from mqt.qudits.visualisation.mini_quantum_information import get_density_matrix_from_counts, partial_trace | ||
from mqt.qudits.compiler.dit_manager import QuditManager | ||
|
||
# In[2]: | ||
|
||
|
||
qasm = """ | ||
DITQASM 2.0; | ||
qreg fields [3][5,5,5]; | ||
qreg matter [2][2,2]; | ||
creg meas_matter[2]; | ||
creg meas_fields[3]; | ||
h fields[2] ctl matter[0] matter[1] [0,0]; | ||
cx fields[2], matter[0]; | ||
cx fields[2], matter[1]; | ||
rxy (0, 1, pi, pi/2) fields[2]; | ||
barrier q[0],q[1],q[2]; | ||
measure q[0] -> meas[0]; | ||
measure q[1] -> meas[1]; | ||
measure q[2] -> meas[2]; | ||
""" | ||
# Control syntax: operation ctl qudit_line [list of qudit control levels] | ||
|
||
circuit = QuantumCircuit() | ||
circuit.from_qasm(qasm) | ||
|
||
print(f"\n Number of operations: {len(circuit.instructions)}, \n Number of qudits in the circuit: {circuit.num_qudits}") | ||
|
||
# In[3]: | ||
|
||
|
||
circuit = QuantumCircuit() | ||
|
||
field_reg = QuantumRegister("fields", 1, [3]) | ||
ancilla_reg = QuantumRegister("ancillas", 1, [3]) | ||
|
||
circuit.append(field_reg) | ||
circuit.append(ancilla_reg) | ||
|
||
print(f"\n Number of operations: {len(circuit.instructions)}, \n Number of qudits in the circuit: {circuit.num_qudits}") | ||
|
||
# In[4]: | ||
|
||
|
||
h = circuit.h(field_reg[0]) | ||
|
||
# Syntax for controlled operations is : | ||
|
||
# h = circuit.h(field_reg[0], ControlData([control_register], [controls_levels])) | ||
# OR | ||
# h = circuit.h(field_reg[0]).control([control_register], [controls_levels]) | ||
|
||
|
||
# In[5]: | ||
|
||
|
||
csum = circuit.csum([field_reg[0], ancilla_reg[0]]) | ||
|
||
# In[6]: | ||
|
||
|
||
print(f"\n Number of operations: {len(circuit.instructions)}, \n Number of qudits in the circuit: {circuit.num_qudits}") | ||
|
||
# In[7]: | ||
|
||
|
||
print(circuit.to_qasm()) | ||
|
||
# In[8]: | ||
|
||
|
||
provider = MQTQuditProvider() | ||
provider.backends("sim") | ||
|
||
# In[9]: | ||
|
||
|
||
backend = provider.get_backend("tnsim") | ||
|
||
job = backend.run(circuit) | ||
result = job.result() | ||
|
||
state_vector = result.get_state_vector() | ||
|
||
plot_state(state_vector, circuit) | ||
|
||
# In[10]: | ||
|
||
|
||
backend_ion = provider.get_backend("faketraps2trits", shots=1000) | ||
|
||
job = backend_ion.run(circuit) | ||
result = job.result() | ||
counts = result.get_counts() | ||
|
||
plot_counts(counts, circuit) | ||
|
||
# In[11]: | ||
|
||
|
||
rho = get_density_matrix_from_counts(counts, circuit) | ||
print(partial_trace(rho, qudits2keep=[0], dims=[3, 3])) | ||
|
||
# In[12]: | ||
|
||
|
||
# the compiler uses energy level graph of the architecture | ||
|
||
qudit_compiler = QuditManager() | ||
passes = ["LocQRPass"] | ||
|
||
# In[13]: | ||
|
||
|
||
compiled_circuit_qr = qudit_compiler.compile(backend_ion, circuit, passes) | ||
|
||
print( | ||
f"\n Number of operations: {len(compiled_circuit_qr.instructions)}, \n Number of qudits in the circuit: {compiled_circuit_qr.num_qudits}") | ||
|
||
# In[18]: | ||
|
||
|
||
job = backend_ion.run(compiled_circuit_qr) | ||
|
||
result = job.result() | ||
counts = result.get_counts() | ||
|
||
plot_counts(counts, compiled_circuit_qr) | ||
|
||
# In[15]: | ||
|
||
|
||
passes = ["LocAdaPass", "ZPropagationPass","ZRemovalPass"] | ||
|
||
compiled_circuit_ada = qudit_compiler.compile(backend_ion, circuit, passes) | ||
|
||
print(f"\n Number of operations: {len(compiled_circuit_ada.instructions)}, \n Number of qudits in the circuit: {compiled_circuit_ada.num_qudits}") | ||
|
||
# In[17]: | ||
|
||
|
||
job = backend_ion.run(compiled_circuit_ada) | ||
|
||
result = job.result() | ||
counts = result.get_counts() | ||
|
||
plot_counts(counts, compiled_circuit_ada) | ||
|
||
# In[ ]: | ||
|
||
|
||
# In[ ]: |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,84 @@ | ||
import numpy as np | ||
|
||
from mqt.qudits.compiler.dit_manager import QuditManager | ||
from mqt.qudits.qudit_circuits.circuit import QuantumCircuit | ||
from mqt.qudits.qudit_circuits.components.instructions.gate_extensions.controls import ControlData | ||
from mqt.qudits.qudit_circuits.components.registers.quantum_register import QuantumRegister | ||
from mqt.qudits.simulation.provider.noise_tools.noise import Noise, NoiseModel | ||
from mqt.qudits.simulation.provider.qudit_provider import MQTQuditProvider | ||
from mqt.qudits.visualisation.run_info import plot_histogram | ||
from mqt.qudits.visualisation.plot_information import plot_counts, plot_state | ||
from mqt.qudits.visualisation.mini_quantum_information import get_density_matrix_from_counts, partial_trace | ||
|
||
qasm = """ | ||
DITQASM 2.0; | ||
qreg q [3][3,2,3]; | ||
qreg bob [2][2,2]; | ||
creg meas[3]; | ||
h q[2]; | ||
h q[2] ctl q[0] bob[0] q[1] [0,0,0]; | ||
cx q[2],q[1]; | ||
cx j[1],q[0]; | ||
rxy ( pi, pi/2 , 0, 1) q[0]; | ||
cx q[1],q[0]; | ||
rxy (0, 1, pi, pi/2) q[0] ctl bob[0] q[1] [0,0]; | ||
barrier q[0],q[1],q[2]; | ||
measure q[0] -> meas[0]; | ||
measure q[1] -> meas[1]; | ||
measure q[2] -> meas[2]; | ||
""" | ||
# c = QuantumCircuit() | ||
# c.from_qasm(qasm) | ||
# print(QASM().parse_ditqasm2_str(qasm)) | ||
qreg_example = QuantumRegister("x", 3, [3, 2, 2]) | ||
c = QuantumCircuit() | ||
c.from_qasm(qasm) | ||
print(c.to_qasm()) | ||
|
||
# Program a quantum algorithm in python | ||
|
||
qreg_example = QuantumRegister("reg", 2, [3, 3]) | ||
circ = QuantumCircuit(qreg_example) | ||
# circ.append(QuantumRegister()) | ||
# circ.from_qasm(qasm) | ||
|
||
# x = circ.x(1) | ||
# s = circ.s(0) | ||
# z = circ.z(1, ControlData([0], [1])) | ||
h3 = circ.h(0) # , ControlData([0], [1]))#.control([1], [1]) | ||
h3 = circ.h(0) | ||
h3 = circ.h(0) | ||
# x = circ.x(0) | ||
# csum = circ.csum([0, 2]) | ||
for i in range(200): | ||
r = circ.r(1, [0, 1, np.pi, np.pi / 2]) | ||
csum = circ.csum([0, 1]) | ||
# cx = circ.cx([0, 1],[0, 2, 1, 0.]) | ||
# rz = circ.rz(0, [0, 1, np.pi / 2]) | ||
# ls = circ.ls([0, 1], [np.pi / 4]) | ||
# ms = circ.ms([0, 1], [np.pi / 2]) | ||
ru = circ.randu([0, 1]) | ||
# p = circ.pm([0], [0, 2, 1]) | ||
|
||
# print(h3.to_matrix(identities=1)) | ||
# print(csum.to_matrix(identities=1)) | ||
|
||
print(circ.to_qasm()) | ||
|
||
provider = MQTQuditProvider() | ||
print(provider.backends("sim")) | ||
# ------------------------------------------------------ | ||
# Noiselse simulation | ||
|
||
provider = MQTQuditProvider() | ||
backend = provider.get_backend("tnsim") | ||
result = backend.run(circ) | ||
|
||
state_size = 1 | ||
for s in circ.dimensions: | ||
state_size *= s | ||
job = backend.run(circ) | ||
result = job.result() | ||
plot_state(result.get_state_vector(), circ) | ||
|
||
backend_ion = provider.get_backend("faketraps2trits", shots=1000) | ||
|
||
job = backend_ion.run(circ) | ||
result = job.result() | ||
plot_counts(result.get_counts(), circ) | ||
|
||
# Evaluate | ||
|
||
rho = get_density_matrix_from_counts(result.get_counts(), circ) | ||
print(np.trace(rho @ rho)) | ||
print(partial_trace(rho, [1], [3, 3])) | ||
|
||
# WE compile the circuit and notice new differences | ||
qudit_compiler = QuditManager() | ||
passes = ["LocAdaPass", "ZPropagationPass", "ZRemovalPass"] | ||
pulse_level_circuit = qudit_compiler.compile(backend_ion, circ, passes) | ||
|
||
result = result.tensor.reshape(1, state_size) | ||
|
||
# takes a vector in | ||
plot_histogram(result, circ) | ||
job = backend_ion.run(pulse_level_circuit) | ||
result = job.result() | ||
plot_counts(result.get_counts(), circ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from abc import ABC, abstractmethod | ||
|
||
|
||
class CompilerPass(ABC): | ||
def __init__(self, backend, **kwargs): | ||
self.backend = backend | ||
|
||
@abstractmethod | ||
def transpile(self, circuit): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from mqt.qudits.compiler.onedit.local_adaptive_decomp import LocAdaPass | ||
from mqt.qudits.compiler.onedit.local_qr_decomp import LocQRPass | ||
from mqt.qudits.compiler.onedit.propagate_virtrz import ZPropagationPass | ||
from mqt.qudits.compiler.onedit.remove_phase_rotations import ZRemovalPass | ||
|
||
|
||
class QuditManager(): | ||
def __init__(self): | ||
pass | ||
|
||
def compile(self, backend, circuit, passes_names): | ||
# Instantiate and execute created classes | ||
for compiler_pass in passes_names: | ||
compiler_pass = globals()[compiler_pass] | ||
decomposition = compiler_pass(backend) | ||
circuit = decomposition.transpile(circuit) | ||
return circuit |
Oops, something went wrong.