Skip to content

Commit 2401718

Browse files
author
Guen Prawiroatmodjo
committed
renaming, refactor QasmListener.measure
1 parent 0f9d266 commit 2401718

File tree

3 files changed

+28
-18
lines changed

3 files changed

+28
-18
lines changed

src/main.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ use std::fs::File;
88
use std::io::prelude::*;
99
use qasm::{process, lex, parse};
1010
use qir_translator::qasm2qir::listener::QasmListener;
11+
use log;
1112

1213
fn main() {
14+
log::info!("Test");
1315
let cwd = env::current_dir().unwrap();
1416
let mut source = String::new();
1517

@@ -40,6 +42,9 @@ fn main() {
4042
}
4143
}
4244

43-
println!("Emitted QIR:");
44-
println!("{:?}", listener.get_ir_string());
45+
// println!("Emitted QIR:");
46+
// println!("{:?}", listener.get_ir_string());
47+
let file_name = "test.ll";
48+
let result = listener.write_model_to_file(file_name.to_string());
49+
println!("{:?}", result);
4550
}

src/qasm2qir/arguments.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
use qasm::Argument;
22

3-
pub struct QubitRef(pub String, pub i32);
4-
impl QubitRef {
3+
pub struct Register(pub String, pub i32);
4+
impl Register {
55
pub fn as_qir_name(&self) -> String {
66
format!("{}{}", self.0, self.1)
77
}
88
}
99

1010
// Needed so that we have a local type to avoid the orphan rule for
1111
// trait coherence.
12-
pub struct Pair<T, U>(pub T, pub U);
12+
pub struct Pair<Register1, Register2>(pub Register1, pub Register2);
1313

14-
impl TryFrom<&Argument> for QubitRef {
14+
impl TryFrom<&Argument> for Register {
1515
type Error = String;
1616

1717
fn try_from(value: &Argument) -> Result<Self, Self::Error> {
1818
match value {
19-
Argument::Qubit(name, idx) => Ok(QubitRef(name.to_owned(), *idx)),
19+
Argument::Qubit(name, idx) => Ok(Register(name.to_owned(), *idx)),
2020
_ => Err(format!("Expected qubit argument, got {:?}.", value))
2121
}
2222
}
2323
}
2424

25-
impl TryFrom<&Vec<Argument>> for QubitRef {
25+
impl TryFrom<&Vec<Argument>> for Register {
2626
type Error = String;
2727

2828
fn try_from(value: &Vec<Argument>) -> Result<Self, Self::Error> {
@@ -33,7 +33,7 @@ impl TryFrom<&Vec<Argument>> for QubitRef {
3333
}
3434
}
3535

36-
impl TryFrom<&Vec<Argument>> for Pair<QubitRef, QubitRef> {
36+
impl TryFrom<&Vec<Argument>> for Pair<Register, Register> {
3737
type Error = String;
3838

3939
fn try_from(value: &Vec<Argument>) -> Result<Self, Self::Error> {

src/qasm2qir/listener.rs

+14-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33
// This code was based on pyqir_generator::python::PyQIR
4-
use pyqir_generator::emit::get_ir_string;
4+
use pyqir_generator::emit::{get_ir_string, write_model_to_file};
55
use pyqir_generator::interop::{
66
ClassicalRegister, Controlled, Instruction, Measured, QuantumRegister, Rotated, SemanticModel,
77
Single,
88
};
99
use qasm::{Argument, AstNode};
1010
use log;
1111

12-
use crate::qasm2qir::arguments::{QubitRef, Pair};
12+
use crate::qasm2qir::arguments::{Register, Pair};
1313

1414
pub struct QasmListener {
1515
pub(super) model: SemanticModel,
@@ -26,6 +26,10 @@ impl QasmListener {
2626
return get_ir_string(&self.model);
2727
}
2828

29+
pub fn write_model_to_file(&mut self, file_name: String ) -> Result<(), String> {
30+
return write_model_to_file(&self.model, &file_name)
31+
}
32+
2933
pub fn cx(&mut self, control: String, target: String) {
3034
log::info!("cx {} => {}", control, target);
3135
let controlled = Controlled::new(control, target);
@@ -159,18 +163,19 @@ impl QasmListener {
159163
}
160164

161165
pub fn measure(&mut self, qubit: Argument, register: Argument) {
162-
if let Argument::Qubit(_name, _idx) = qubit {
163-
if let Argument::Register(_reg_name) = register {
164-
self.m(format!("{}{}", _name, _idx), format!("{}0", _reg_name));
165-
}
166-
}
166+
let qubit: Register = (&qubit).try_into().unwrap();
167+
let register: Register = (&register).try_into().unwrap();
168+
self.m(
169+
qubit.as_qir_name(),
170+
register.as_qir_name()
171+
);
167172
}
168-
173+
169174
pub fn apply_gate(&mut self, name: String, qubits: Vec<Argument>) {
170175
println!("{:?}", name);
171176

172177
if name == "h" {
173-
let qubit: QubitRef = (&qubits).try_into().unwrap();
178+
let qubit: Register = (&qubits).try_into().unwrap();
174179
self.h(qubit.as_qir_name());
175180
} else if name == "CX" {
176181
let Pair(control, target) =

0 commit comments

Comments
 (0)